JQuery复选框复制值不起作用

时间:2012-10-02 17:34:09

标签: jquery

我写了这个表格,它工作正常,除了应该复制地址值的复选框。无论如何,我如何写,重写和纠正它。它根本行不通。

代码的第一部分是表单,第二部分是代码的复制部分,第三部分是不相关的掩码。

$(document).ready(function () {
//Any hidden errors that were passed back from 4D?
// for signup forms, we need to see which form we will show the errors on
if ($("#referralForm1").is(":visible")){
    show4DErrors("#referralForm1"); 
}

$("#referralForm1").validate({
    rules: {
        Referral_Name_T: "required",
        Referral_Company_T: "required",
        Referral_Address_T: "required",
        Referral_City_T: "required",
        Referral_State_T: "required",

        Referral_Zip_T: {
            required: true,
            zipCode: true
        },

        Referral_PhoneFull_T: {
            required: true,
            phoneNumber: true
        },

        Referral_Email_T: {
            required: true,
            email: true,

        },
        Referral_EmailConfirm_T: {
            required: true,
            email: true,
            equalTo: "#Referral_Email_T"
        },

        Referral_PayToName_T: "required",
        Referral_PayToAddress_T: "required",
        Referral_PayToCity_T: "required",
        Referral_PayToState_T: "required",

        Referral_PayToZip_T: {
            required: true,
            zipCode: true
        },

    }
});


$("Referral_SameAsAbove_T").click(function(){ 
    if ($("Referral_SameAsAbove_T").is(':checked')) 
    { 
        // Checked, copy values 
        $("#Referral_PayToAddress_T").val($("#Referral_Address_T").val()); 
        $("#Referral_PayToCity_T").val($("#Referral_City_T").val()); 
        $("#Referral_PayToState_T").val($("#Referral_State_T").val()); 
        $("#Referral_PayToZip_T").val($("#Referral_Zip_T").val()); 
    } 
    else 
    { 
        // Clear on uncheck 
        $("#Referral_Address_T").val(""); 
        $("#Referral_City_T").val(""); 
        $("#Referral_State_T").val(""); 
    } 
}); 




//Mask Setup
$('#Referral_PhoneFull_T').mask('(999) 999-9999');
$('#Referral_Zip_T').mask('99999');
$('#Referral_PayToZip_T').mask('99999');

});

1 个答案:

答案 0 :(得分:3)

看起来你的选择器错了

$("Referral_SameAsAbove_T")

If it is a class then supposed to be   $(".Referral_SameAsAbove_T") // prepended by a dot

If it is a id then supposed to be   $("#Referral_SameAsAbove_T") // prepended by a pound

修改

处理文本框的更改事件..

$('input[type="text"]').on('change' , function() {
    if ($("Referral_SameAsAbove_T").is(':checked')) {
         if( this.id.indexOf('Address') > -1){
            $("#Referral_PayToAddress_T").val($(this).val());
         }

          if( this.id.indexOf('City') > -1){
            $("#Referral_PayToCity_T").val($(this).val());
         }
       // Other textbox conditions here

    }
    else{
        $(this).val('');
    }

});

FIDDLE

注意:这只是为了处理PayToAddress_T文本框的更改事件。对于其他人的