开发者

Rails3 setting focus on a textfield

开发者 https://www.devze.com 2023-01-08 12:32 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消