选择多个累积数据,如何清理?

时间:2016-01-10 07:26:13

标签: javascript jquery json ajax forms

帮助,当我选择一个客户时,我有一个自动填充数据的表单,包括他们的地址,地址被加载到选择的多个。问题是这样的:当我选择另一个客户时,表单没有完全清理,以前客户的地址作为当前客户的地址被累积在选择的多个中。是否可以在选择其他客户时自动清理整个表单或至少选择多个表单?感谢。

$('input[name=\'customer\']').catcomplete({
 minLength: 0, delay: 100,
source: function(request, response) {
    $.ajax({
        url: 'index.php?route=sale/customer/autocomplete&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request.term),
        dataType: 'json',
        success: function(json) {   
            response($.map(json, function(item) {
                return {
                    category: item['customer_group'],
                    label: item['name'],
                    value: item['customer_id'],
                    customer_group_id: item['customer_group_id'],
                    firstname: item['firstname'],
                    lastname: item['lastname'],
                    rut_cliente_id: item['rut_cliente_id'],
                    email: item['email'],
                    telephone: item['telephone'],
                    fax: item['fax'],
                    banca: item['banca'],
                    company: item['company'],
                    iban: item['iban'],
                    address: item['address']
                }
            }));
        }
    });
}, 

select:function(event,ui){
    event.preventDefault()
    $('input[name=\'customer\']').val(ui.item.label);
    $('input[name=\'customer_id\']').val(ui.item.value);
    $('input[name=\'firstname\']').val(ui.item.firstname);
    $('input[name=\'lastname\']').val(ui.item.lastname);
    $('input[name=\'rut_cliente_id\']').val(ui.item.rut_cliente_id);
    $('input[name=\'email\']').val(ui.item.email);
    $('input[name=\'telephone\']').val(ui.item.telephone);
    $('input[name=\'banca\']').val(ui.item.banca);
    $('input[name=\'iban\']').val(ui.item.iban);
    $('input[name=\'company\']').val(ui.item.company);
    $('input[name=\'fax\']').val(ui.item.fax);




    for(i in ui.item.address){ 

        html+='<option value="' + ui.item.address[i]['address_id']+'">'+ui.item.address[i]['firstname']+' ' + ui.item.address[i]['lastname']+', ' + ui.item.address[i]['address_1']+', ' + ui.item.address[i]['city']+', ' + ui.item.address[i]['country']+'</option>';
    }

    $('select[name=\'shipping_address\']').html(html);
    $('select[name=\'payment_address\']').html(html);

    $('select[id=\'customer_group_id\']').removeAttr('disabled').val(ui.item['customer_group_id']).change().attr('disabled',true); 


    return false; 

}});

1 个答案:

答案 0 :(得分:0)

在设置新值之前使用此代码

$("input").each(function(){
   $(this).val("");
});
$('select[name=\'shipping_address\']').html("");
$('select[name=\'payment_address\']').html("");
html = ""