English 中文(简体)
铁路用JQuery滴滴网张贴控制器
原标题:Posting to controller using JQuery drop-down box in Rails 3.1

I have the following HTML dropdown box on the index view of the Search controller. I want to update the view based upon the dropbox selection. I need to pass the selection to the controller somehow.

<!-- index.html rendered from Search/Index -->
<select id="search_params" name="search[params]">
   <option value="tacos">tacos</option>
   <option value="pizza">pizza</option>
</select>

<>Search 主计长代码采用准绳和滚动信息

def index
  query = params[:search]
  msg = "Your favorite food is #{query}! OMGWTFBBQ!?!?!?!"

  respond_to do |format|
    format.html
    format.js
  end
end

This Javascript should somehow post the dropdown box choice to the controller upon selection:

$("#search_params").change(function() {
  var state = $( select#search_params :selected ).val();
  if(state == "") state="0";
   //Send the selection to the controller Search controller somehow 
   // and then render a new view immediately ?
   //I AM NOT GOOD WITH COMPUTER
})
return false;
});

I ve looked at dozens of other Rails + JQuery examples, jquery API docs, etc. Can t find an answer to the simple question or I m just overthinking it.

Anyways, thanks in advance for your help! ~Dan

最佳回答

I m no Duncan/Rails guy,但我知道 j。

I think you want something like

$("#search_params").change(function() {
  var state = $( select#search_params :selected ).val();
  if(state == "") {
     state="0";
  }
  //Call jQuery AJAX
  $.ajax({
       url: /path/to/your/controller/action ,
       type: POST ,
       data: search=  + state,
       success: function(data) {
           alert(data);
       },
       error: function() { 
          alert( Stuff went wrong );
       }
    });
  return false;
});

Hope this helps, and have a read of the jQuery AJAX doc page.

我不真地确定,你如何从那里控制人员收回数据? 是否有相应的观点使<条码>sg?

问题回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签