Rails3 setting focus on a textfield
When I have errors in a form, I would like to set the focus to the first textfield with the error. I've done this with PHP and JavaScript in the past开发者_JAVA百科, but I'm not sure how to approach it in rails3.
Thanks in advance
<%= javascript_tag "$('foo_bar').focus()" %>
Although many people (in other articles too) suggested:
<%= javascript_tag "$('foo_bar').focus()" %>
When I first tried that, it didn't work for me. But I didn't look into why because the second thing I tried did work:
<%= f.email_field :email, :autofocus => true %>
This article points out that's an HTML5 approach: How to Focus a Form Input...
I had to do
<%= javascript_tag "$('foo_bar').focus()" %>
to get this to work (note the parentheses on the end of the focus
method.
精彩评论