2
我有一個選擇菜單中定義爲Ruby on Rails的形式如下:回報率:form.select +平變化沒有通過右值中PARAMS到控制器
<%= f.select :Menu1, [["Option1","value1"],["Option2","value2"]], {}, {:id=>"Menu1_Id", :class => "Menu1_Class"} %>
我使用的事件處理程序,以觸發當一個選項被選中控制器動作,我想傳遞給它選擇的選項的值
<script type="text/JavaScript" src=http://code.jquery.com/jquery-latest.js">
$(function(){
$('.Menu1_Class').bind('change', function(){
alert($(this).val());
$.ajax('#{:controller => "TestsController", :action => "show"}?param_one='+$(this).val());
});
});
</script>
編輯:這是低於感謝爲我工作羅賓的答案的js代碼(請注意,我用它採取行動「get_results」而不是原來的「秀」):
<script type="text/JavaScript">
$(function(){
$('.menu_class').bind('change', function(){
$.ajax({
url: "<%= get_results_my_tests_url %>",
data: {
param_one: $(this).val()
}
});
});
});
</script>
在我的控制器
# GET /tests/1
# GET /tests/1.json
def show
@test = Test.find(params[:id])
if params[:param_one].present?
@blah=params[:param_one]
puts "show : @blah = " + @blah.to_s + "\n"
end
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @test }
end
end
有了這個
,警報消息包含正確的值(值1或值2),但param_one不存在這樣把回報沒有爲「param_one」,而我希望請參閱「value1」或「value2」。
任何人都可以請我指我在哪裏做錯了什麼?
感謝
感謝羅賓,現在都工作正常! – rh4games 2012-01-02 00:36:48