2015-08-19 33 views
0

嘿傢伙我試圖讓輸入框有焦點,當頁面加載。這樣用戶就可以簡單地開始在框中輸入而不用標籤了。另外,如果您有咖啡標記版本,我也很樂意看到這一點。謝謝!rails問題與document.getElementById(「id」)。焦點()

我有這個在mailer.js文件:

document.getElementById("id").focus() 

這個網站在process_order.html.erb

<%= form_tag(:action => 'process_orders', :method => "get") do %> 
<%= text_field_tag :id %> 
<%= submit_tag "Search", :name => nil %> 
<% end %> 

這裏是頁

<form action="/process?method=get" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="JE2OKhdKm+vgUufwB8mIcPtVQgTAktzahT1MkLGvDbVWb+b5mTuAyZ1L6ATgcWy9l8285R21Qqm KWt/BCt4/QA==" /> 
<input type="text" name="id" id="id" /> 
<input type="submit" value="Search" /> 
</form></br> 
+1

待辦事項你有在文檔就緒功能? – code

回答

0

autofocus是一個HTML5特性,它可能不被所有瀏覽器都支持

您可以在window.onload事件

window.onload = function() { 
    document.getElementById("id").focus(); 
} 

這裏設置焦點是CoffeeScript的版本

window.onload = -> 
    document.getElementById('id').focus() 
    return 
+0

好吧,我們更接近,它重點放在形式,當我點擊重新加載,但不是當我點擊鏈接導致搜索.. ... – ja11946

0
的源代碼

您可以將autofocus屬性添加到您的文本字段。只要改變

<%= text_field_tag :id %> 

<%= text_field_tag :id, nil, autofocus: true %> 

,你應該得到所期望的功能。

+0

是的,我知道這個命令,但我試圖解決非html5瀏覽器 – ja11946