2つのドロップダウンリストがあります。人と企業のためのもの。リストから人を選択すると、データベースに自動的に照会され、その人に関連付けられている適切な対応するビジネスを選択します。ビジネスを選択すると、その企業に関連付けられているすべての人物のデータベースが自動的に照会されます。次に、現在の人のリストをクリアし、関連する人を追加します。ループでスタックに陥る。 - Select2ドロップダウンリスト
これはすべて素晴らしいです...私がドロップダウンリストを簡単に検索可能にするためにSelect2を使いたいまでは。私は今Select2が.val()。trigger( 'change')メソッドを使用して必要な値を選択するので、無限ループに陥るでしょう。これがトリガされると、クエリーを実行してフィールドを移入する.change関数もトリガされます。
私はこれを修正するために何ができますか?ここで
は私のコードです:自分の前にSelectセレクトを使用した未
$('#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);
})
});
}
});
ありがとうございました!それが私の問題を解決しました。 – Mason