通过ajax请求访问select_tag ruby​​小部件中的数据以更新数据

时间:2016-06-13 15:58:35

标签: jquery ruby-on-rails ruby json ajax

我在haml文件中有以下select_tag:

= select_tag 'setup_form_popup', options_for_select([ ["No Setup Time","0"],
["15 Minute", "15"], ["30 Minute", "30"], ["45 Minute", "45"], 
["60 Minute", "60"], ["75 Minute", "75"], ["90 Minute","90"], 
["105 Minute", "105"],["120 Minute","120"]], @event.setup_time), 
{:prompt => 'Setup Time'}

如何通过ajax请求中的数据属性访问服务器?以下代码段:

        var setup_time_update = $.ajax({
            url: '/calendar',
            dataType: 'json',
            type: 'PUT',
            data: "What do I put here?";
        });

ajax的新手仍然在学习绳索。很棒的技术。欢迎任何和所有帮助。干杯〜

1 个答案:

答案 0 :(得分:0)

// Get value of select tag
var setup_form_popup_value = $("#setup_form_popup").val();

$.ajax({
  url: '/calendar',
  dataType: 'json', // Are you expecting json from server? If you want to execute javascript returned from server then dataType should be 'script'.
  method: 'PUT', 
  data: { setup_time: setup_form_popup_value } // You will get params[:setup_time] in your controller.
});

您可以参考jQuery.ajax()文档。和 #136 jQuery & Ajax (revised)