【问题标题】:Ruby on rails - update input select options using AJAXRuby on rails - 使用 AJAX 更新输入选择选项
【发布时间】:2015-11-03 16:49:40
【问题描述】:

我有这个输入的简单表单:

<%= f.input :seleced_seats, collection: @event.seats, label: "-", label_html: { style: 'visibility: hidden; height: 0px;'}, include_blank: false %>

它包含我想使用 AJAX 更新的选项。我获取输入数组(data.seats)的 JavaScript 是:

$("#event_seleced_seats").change(function(){
    var seleced_seats = $(this).val();  
    var seleced_term = $("*[class='list-group-item active']").attr('id');

    jQuery.getJSON(window.location.href + "/price", {edition: seleced_seats, term: seleced_term}, function(data) {
        $("#price").text(data.total_price);
        $("#termA").text(data.seats);
        console.log(data.seats);
    });
});

如何使用 data.seats 中的数据重绘 f.input?

【问题讨论】:

    标签: javascript ruby-on-rails ajax ruby-on-rails-4 simple-form


    【解决方案1】:

    清空下拉列表并使用 jQuery append 添加 data.seats 怎么样?

    所以是这样的:

    (假设 data.seats 将返回一个座位对象数组)

    var dropDown = $(yourdropdown);
    dropDown.empty();
    
    data.seats.forEach(function(seat){
      dropDown.append("<option value='"+seat.your_value+"'>"+seat.your_value+"</option>")
    });
    

    您也可以使用 jQuery 的 each 方法来遍历集合。 有关详细信息,请参阅 API:http://api.jquery.com/jquery.each/

    【讨论】:

      猜你喜欢
      • 2013-05-15
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      相关资源
      最近更新 更多