【发布时间】:2016-06-03 02:18:33
【问题描述】:
我有 2 个下拉列表。一种用于人,一种用于企业。如果您从列表中选择一个人,它将自动查询数据库,然后选择与该人关联的正确对应业务。如果您选择一个企业,它将自动查询与该公司关联的所有人员的数据库。然后它会清除当前人员列表并附加相关人员。
这一切都很好......直到我想使用 Select2 使我的下拉列表易于搜索。我现在陷入无限循环,因为 Select2 使用 .val().trigger('change') 方法来选择您想要的值。当它被触发时,它还会触发运行查询并填充字段的 .change 函数。
我能做些什么来解决这个问题?
这是我的代码:
$('#personNameField').select2();
$('#businessNameField').select2();
/* When a business is selected it then pulls all the associated customers and puts them into the customer name drop down list */
$("#businessNameField").change(function getAssociatedPeople() {
var sitePath = sitepath.sitePath;
var business_id = $("#businessNameField").val();
if (business_id == 'Choose a Company') {
$('#personNameField').val('Choose a Person').trigger('change');
}
else {
/* Location of the query script that pulls info from the database */
var url = myticketsscript.pluginsUrl + '/portal/public/includes/shortcodes/my-tickets/auto-complete/auto-complete.php';
$.get( url, { business_id: business_id, sitePath: sitePath } )
.done(function( data ) {
$('#personNameField').html('<option value="Choose a Person">Choose a Person</option>');
var results = jQuery.parseJSON(data);
console.log(data);
$(results).each(function(key, value) {
/* Add the data to the customer name drop down list */
$('#personNameField').append('<option id="customer_id" value="'+ value.id +'">' + value.first_name + ' ' + value.last_name + '</option>');
/* Logs the data in the console */
console.log(key);
console.log(value);
})
});
}
});
/* When a person is selected it then pulls all the associated business and puts it into the business name drop down list */
$("#personNameField").change(function() {
var customer_id = $("#personNameField").val();
var sitePath = sitepath.sitePath;
if (customer_id == 'Choose a Person') {
var url = myticketsscript.pluginsUrl + '/portal/public/includes/shortcodes/my-tickets/auto-complete/auto-complete.php';
$.get( url, { customer_list: customer_id, sitePath: sitePath } )
.done(function( data ) {
$('#businessNameField').val('Choose a Company').trigger('change');
$('#personNameField').html('<option value="Choose a Person">Choose a Person</option>');
var results = jQuery.parseJSON(data);
console.log(data);
$(results).each(function(key, value) {
/* Add the data to the customer name drop down list */
$('#personNameField').append('<option id="customer_id" value="'+ value.id +'">' + value.first_name + ' ' + value.last_name + '</option>');
/* Logs the data in the console */
console.log(key);
console.log(value);
})
});
}
else {
/* Location of the query script that pulls info from the database */
var url = myticketsscript.pluginsUrl + '/portal/public/includes/shortcodes/my-tickets/auto-complete/auto-complete.php';
$.get( url, { customer_id: customer_id, sitePath: sitePath } )
.done(function( data ) {
var results = jQuery.parseJSON(data);
console.log(data);
$(results).each(function(key, value) {
/* Selects the correct company for the customer selected */
$('#businessNameField').val(value.id).trigger('change');
console.log(key);
console.log(value);
})
});
}
});
【问题讨论】:
标签: javascript jquery mysql select2