2015-11-03 109 views
0

更新输入选择选项我有简单的形式与该输入:Ruby on Rails的 - 使用AJAX

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

它包含了我想用AJAX来更新选项。 JavaScript的,我得到的输入(data.seats)数组是:

$("#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?

回答

2

如何清空下拉菜单并添加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的每个方法遍历集合。 查看API获取更多信息:http://api.jquery.com/jquery.each/