-1

I am trying to get the value user selected from search prompt and pass it to javascript. in my search controller it's stored in @result. I am then finding what I need with (params[:query_id]

Wanted to make prompt for unsigned users a bit fancier by adding their selection to prompt. Tried it a simple way -

var selected = getElementById('#query').value;

But got this error message: ReferenceError: Can't find variable: getElementById

Tried to get this variable from a ruby array:

var selected= <%= @result.to_json %>;

But it returns null

Adding searh controller's code, as suggested:

def index respond_to do |format| 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

Then in search html I have

#search
  = form_tag find_from_query_entries_path, :remote => true, :id => "search_form" do
    = text_field_tag :query, nil
    = hidden_field_tag :query_id
4

2 回答 2

2

getElementById未在全局对象中定义。它是 a 的属性document

尝试:

var selected = document.getElementById('#query').value;
于 2012-06-22T22:26:29.253 回答
1

Rails 自带 jQuery,为什么不使用它呢?

我想这就是你想要的。

$('query').val()

但是您应该在浏览器中跳转到呈现的 html 以检查您是否要选择作为名称查询的节点(我认为这是 rails 所做的或您认为的 id 查询

于 2012-06-22T22:57:14.243 回答