如何在克隆时为所选的选择设置默认值?

时间:2017-05-01 08:44:32

标签: javascript jquery

HTML:

<select data-placeholder="Choose a Travel Agency..." class="chosen-select fc-inputbox travel-agency-dropdown" id="travel_agency_id">
    ... foreach
</select>

使用Javascript:

var data = {
    id: row.find('td:first-child').text().trim(),
    travel_agency_name: row.find('td:nth-child(2)').text().trim(),
};
var select_travel_agency = $('.travel-agency-dropdown:first').clone().attr('id', 'travel-agency-id-'+data.id).val(data.travel_agency_name).trigger("chosen:updated");

我已经创建了旅行社,我想要的是在设置代理商的SELECT默认值时更新它。我克隆它的原因是我获取数据而不调用另一个GET

1 个答案:

答案 0 :(得分:0)

你能使用jQuery的change函数吗? 例:

<select class="target">
    <option value="option1" selected="selected">Option 1</option>
    <option value="option2">Option 2</option>
  </select>
$( ".target" ).change(function() {
  alert( "Handler for .change() called." );
});

所以你只需要写:

var clonedSelect = $('#travel-agency-id-'+data.id);
clonedSelect.change(function(){alert('second select changed his value to ' + clonedSelect.val());});