我想填充幾個使用jQuery相同類的選擇框。這是我的代碼jquery選擇選項默認選項文本
populateFieldMapping: function (data,obj) {
jQuery('.field-mapping select').each(function()
{
var option = '<option value="" data-custom-id="_select_">' + "please select a field" + '</option>';
jQuery.each(data, function (i, res) {
option += '<option value="' + res.id + '" data-custom-id="' + dataID + '" data-custom-name="' + res.name + '">' + res.name + '</option>'
});
$(this).html(option);
obj.select2();
});
},
我的HTML
<div class="field-mapping">
<select id="podio-fields-mapping" class="form-control" tabindex="-1">
</select>
<select id="podio-fields-mapping" class="form-control" tabindex="-1">
</select></div>
一切工作正常,除了我只收到「請選擇一個字段」默認只有第一個選擇框中選擇。什麼可能是錯的? 我在每個選擇框中獲取所有值。
obj = $('.form-control');
你能顯示HTML使用jQuery來? –
'obj.select2();'中的'obj',不能是'$(this).select2();'? –
也使用.append而不是.html – mplungjan