Javascript ajax调用会在后续单击时导致重复数据
我有一个弹出式菜单,可以从中选择客户。它由一个ajax调用填充,并在表单上更新选择。但是,当我再次单击.lookup cust hide时,我的客户列表中的json数据会自动重复,这很好。我现在有两个完整的客户列表 我在想,我需要以某种方式在最后清除ajax调用 javascript:Javascript ajax调用会在后续单击时导致重复数据,javascript,ajax,Javascript,Ajax,我有一个弹出式菜单,可以从中选择客户。它由一个ajax调用填充,并在表单上更新选择。但是,当我再次单击.lookup cust hide时,我的客户列表中的json数据会自动重复,这很好。我现在有两个完整的客户列表 我在想,我需要以某种方式在最后清除ajax调用 javascript: $("#customer .lookup-cust-hide").click(function() { $("#contactdiv1").css("display", "block");
$("#customer .lookup-cust-hide").click(function() {
$("#contactdiv1").css("display", "block");
$.ajax({
url: 'customer_fill.php',
data: {action:"invoice"},
dataType: 'json',
success: function(data){
populateSelectBoxes($('#contactdiv1 #dropdown'), data);
function populateSelectBoxes($select, data) {
var customers = [];
$.each(data, function() {
customers.push('<li data-value="'+this.autonum+'">' + this.customer + '</li>');
});
$select.append(customers.join(''));
}
function populateTableRow($tableBody, data, selectedCustomerAutonum) {
var customers;
$.each(data, function() {
if (this.autonum == selectedCustomerAutonum) {
customers = this;
return false;
}
});
$($tableBody).val(customers.customer+ '\n' + customers.address1 + '\n' + customers.address2 + '\n' + customers.address3);
}
$('#contactdiv1 #dropdown li').click(function(e) {
var selection = $(this).attr("data-value");
$(this).parent().parent().parent().hide();
populateTableRow($('#customer-title'), data, selection);
});
}
});
});
$("#contact #cancel").click(function() {
$(this).parent().parent().hide();
});
需要补充:
$("#contact #cancel").click(function() {
$('ul').empty();
$(this).parent().parent().hide();
});
…最终结果:
$("#customer .lookup-cust-hide").click(function() {
$("#contactdiv1").css("display", "block");
$.ajax({
url: 'customer_fill.php',
data: {action:"invoice"},
dataType: 'json',
success: function(data){
populateSelectBoxes($('#contactdiv1 #dropdown'), data);
function populateSelectBoxes($select, data) {
var customers = [];
$.each(data, function() {
customers.push('<li data-value="'+this.autonum+'">' + this.customer + '</li>');
});
$select.append(customers.join(''));
}
function populateTableRow($tableBody, data, selectedCustomerAutonum) {
var customers;
$.each(data, function() {
if (this.autonum == selectedCustomerAutonum) {
customers = this;
return false;
}
});
$($tableBody).val(customers.customer+ '\n' + customers.address1 + '\n' + customers.address2 + '\n' + customers.address3);
}
$('#contactdiv1 #dropdown li').click(function(e) {
var selection = $(this).attr("data-value");
$(this).parent().parent().parent().hide();
populateTableRow($('#customer-title'), data, selection);
$('ul').empty();
});
}
});
});
$("#contact #cancel").click(function() {
$('ul').empty();
$(this).parent().parent().hide();
});
是清除选择框,如果您愿意,还可以缓存ajax调用如何清除框?$'select'。空;我的选择框实际上不是一个元素-它是一个元素,我尝试过:$'dropdown'。empty;倒空瓶子,但没用?空的工作很好