Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 将数据从一个字段复制到另一个字段_Jquery_Razor_Knockout.js - Fatal编程技术网

Jquery 将数据从一个字段复制到另一个字段

Jquery 将数据从一个字段复制到另一个字段,jquery,razor,knockout.js,Jquery,Razor,Knockout.js,我对淘汰JavaScript和Jquery非常陌生,我正在构建一个新表单,其中我有两个块,一个是发货地址,一个是账单地址。如果账单地址与发货地址相同,我有一个复选框,需要将地址字段数据从发货地址字段复制到账单 如果发货地址和账单地址相同,请选中此框。 送货地址: 电邮: 名字: 帐单地址: 电邮: 名字: 是否有一种简单的方法可以在点击复选框时将数据从发货地址字段映射到账单地址字段。if($('SameAsShippingAddress').prop('checked')==true){$

我对淘汰JavaScript和Jquery非常陌生,我正在构建一个新表单,其中我有两个块,一个是发货地址,一个是账单地址。如果账单地址与发货地址相同,我有一个复选框,需要将地址字段数据从发货地址字段复制到账单


如果发货地址和账单地址相同,请选中此框。
送货地址:
电邮:
名字:
帐单地址:
电邮:
名字:

是否有一种简单的方法可以在点击复选框时将数据从发货地址字段映射到账单地址字段。

if($('SameAsShippingAddress').prop('checked')==true){$('EmailCompetitor_billing').val($('EmailCompetitor').val();}else{$('EmailCompetitor_billing').val('))}

编写一个复选框更改函数:

$('#SameAsShippingAddress').change(function () {
                if ($(this).is(':checked')) {
                    $("#EmailCompetitor_Billing").attr("value", $("#EmailCompetitor").val());
                } else {
                    //do sth
                }
            });

通常处理此问题的方法是使用更改功能并检查复选框是否已选中:

// Listen for when the object with ID SameAsShippingAddress  (input type:checkbox) changes
$("#SameAsShippingAddress").on("change", function (e) {
    // Is the checkbox checked?
    if(this.checked) {
        // If checked, put the shipping address information into the billing address information
        $("#EmailCompetitor_Billing").val($("#EmailCompetitor").val());
        $("#FirstNameCompetitor_Billing").val($("#FirstNameCompetitor").val());
    }
});
注意:您可以通过添加else语句来清除数据,因此整个代码如下所示:

// Listen for when the object with ID SameAsShippingAddress (input type:checkbox) changes
$("#SameAsShippingAddress").on("change", function (e) {
    // Is the checkbox checked?
    if(this.checked) {
        // If checked, put the shipping address information into the billing address information
        $("#EmailCompetitor_Billing").val($("#EmailCompetitor").val());
        $("#FirstNameCompetitor_Billing").val($("#FirstNameCompetitor").val());
    } else {
      // (if not checked / unchecked) Remove billing information
      $("#EmailCompetitor_Billing").val(""));
      $("#FirstNameCompetitor_Billing").val("");
    }
});
…但这可能会导致糟糕的用户体验,因为用户可能在选中该框后已经填写了信息或略微编辑了发货地址信息

// Listen for when the object with ID SameAsShippingAddress (input type:checkbox) changes
$("#SameAsShippingAddress").on("change", function (e) {
    // Is the checkbox checked?
    if(this.checked) {
        // If checked, put the shipping address information into the billing address information
        $("#EmailCompetitor_Billing").val($("#EmailCompetitor").val());
        $("#FirstNameCompetitor_Billing").val($("#FirstNameCompetitor").val());
    } else {
      // (if not checked / unchecked) Remove billing information
      $("#EmailCompetitor_Billing").val(""));
      $("#FirstNameCompetitor_Billing").val("");
    }
});