ajax调用会导致后续点击重复数据

时间:2017-02-13 15:07:26

标签: javascript ajax

我有一个弹出菜单,我从中选择客户。它填充了一个ajax调用,并在表单上更新选择。这很好,但是当我第二次点击.lookup-cust-hide时,json数据(我的客户列表)会重复。我现在有两个连续的客户列表..

我想我最后需要以某种方式清除ajax调用???

的javascript:

$("#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();
    });

1 个答案:

答案 0 :(得分:0)

需要添加:

$("#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();
});