我正在尝试将JQuery更改事件处理程序添加到select中,但问题是它也会在页面加载时多次触发(等于选项数)。选项是从数据库动态设置的。
html:
<div class="form-group">
<div class="col-sm-3">
<div class="sandbox">
<select id="fct_address_country_id" class="form-control input-sm">
<select>
</div>
</div>
</div>
JQuery的:
$( document ).ready(function() {
$('#fct_address_country_id').change(onclick_fct_address_country_id);
populateSelect2_singleItemPredefined ('fct_address_country_id','select id, name from countries where id = 1 or id = 2');
});
var onclick_fct_address_country_id = function() {
alert ('edgar');
};
var populateSelect2_singleItemPredefined = function(id, sql_query_str, onChangeHandler){
sqlQueryJSON_result(sql_query_str).done (function(obj)
{
$(document.getElementById(id)).selectize({persist: false,
maxOptions: 6});
var selectize_tags = $(document.getElementById(id))[0].selectize;
selectize_tags.addOption({
value: '',
text: 'Select value'
});
for (var i = 0; i< obj.length; i++)
{
selectize_tags.addOption({
value: obj[i][0],
text: obj[i][1]
});
selectize_tags.addItem(obj[i][0]);
}
});
}