javascript - Ruby on Rails - 使用 AJAX 更新输入选择选项

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

我有带有此输入的简单表单:

<%= 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?

最佳答案

清空下拉列表并使用 jQuery 附加添加 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 every 方法来循环访问集合。 请参阅 API 了解更多信息:http://api.jquery.com/jquery.each/

关于javascript - Ruby on Rails - 使用 AJAX 更新输入选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33504728/

相关文章:

ruby-on-rails - capybara 中的精确匹配

javascript - 使用ajax上传图片和表单提交

c# - 为什么 JSON 中日期时间的行为在不同系统(win xp、server 2003)上不同?

javascript - 具有多个输入的 jQuery 自动完成 - django-dynamic-formset

javascript - 我正在导入和调用的功能未在 React 应用程序中运行

mysql - ActiveRecord 是否更改了我的序列化哈希上的编码

jquery - 在ajax成功函数中使用$(this)

javascript - 使用 Google Apps 脚本执行 SHA256withRSA

javascript - BigQuery 在 Javascript UDF 中使用 Regex 转义动态值

jquery - 使用 ERB/jquery 链接到下一页