我想填充几个使用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