[英]passing variables form ruby to javascript
我試圖從搜索提示中選擇值用戶,並將其傳遞給javascript。 在我的搜索控制器中,它存儲在@result中。 然后,我正在尋找所需的(params [:query_id]
希望通過將未簽名的用戶的選擇添加到提示中來為未簽名的用戶提供提示。 嘗試了一個簡單的方法-
var selected = getElementById('#query').value;
但是收到了以下錯誤消息:ReferenceError:找不到變量:getElementById
試圖從紅寶石數組獲取此變量:
var selected= <%= @result.to_json %>;
但它返回null
按照建議添加searh控制器的代碼:
def index response_do |格式| format.html {render:text =>“ html”}
format.js do
@search = SearchItem.search{ keywords params["query"] }
@result = []
@search.each_hit_with_result do |hit, item|
@result << {:name => hit.stored(:name), :id => item.id}
end
render :json => @result.to_json
end
end
然后在搜索HTML我有
#search
= form_tag find_from_query_entries_path, :remote => true, :id => "search_form" do
= text_field_tag :query, nil
= hidden_field_tag :query_id
未在全局對象中定義getElementById
。 它是document
的屬性。
嘗試:
var selected = document.getElementById('#query').value;
jQuery附帶了Rails,為什么不使用它呢?
我想這就是你想要的。
$('query').val()
但是,您應該在瀏覽器中跳入呈現的html,以檢查是否要選擇作為名稱查詢的節點(至於我認為這是Rails所做的,或者是id的查詢,因為您認為是)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.