Billing address matches jQuery delivery address

I am having trouble getting the billing address to copy through the delivery address using jQuery. I have successfully done this using a simple jane form without any custom jQuery elements. But when I add the user interface to the checkbox, it seems to break the code. I tried several code changes, but none of them work.

When a user clicks "My billing address matches my delivery address", nothing happens. Here is my jQuery code:

<script type="text/javascript">
$(document).ready(function(){ 
    $('#check-address').click(function(){
        if($('#check-address').attr('checked')){
            $('#address-field1').val($('#address-field').val());
            $('#city-field1').val($('#city-field').val());
            $('#zip-field1').val($('#zip-field').val());
            var state = $('#state-field option:selected').val();
            $('#state-field1 option[value=' + state + ']').attr('selected','selected');
        } else { 
            //Clear on uncheck
            $('#address-field1').val("");
            $('#city-field1').val("");
            $('#zip-field1').val("");
            $('#state-field1 option[value=Nothing]').attr('selected','selected');
        };

    });
});
</script>

Any help is much appreciated!

+5
source share
6 answers

Parameter Demonstration

$('#check-address').attr('checked')

should be

$('#check-address').is(':checked')


, . http://jsfiddle.net/sRt6G/1/ this.checked .
+4

, , ( ), <input type="checkbox"/> div .

click, change, ;

$('#check-address').change(/* function */);

, attr('checked') prop('checked'), . jQuery prop(); http://api.jquery.com/prop

+5

jquery 1.7. prop attr

  $('#check-address').prop('checked')



       $(elem).attr('checked')    // returned true (Boolean) prior to 1.6
        $(elem).attr('checked')   // now returns "checked" (string) versions after 1.6
        $(elem).prop('checked')    // now returns true (Boolean) versions after 1.6
+4

, . :

$('#check-address').on('change', function (e) {...

 if ($('#check-address').is(":checked")....
+1

.

$('input[name=city\\[' + this.value + '\\]]').val($('input[name=city\\[1\\]]').val());

, , .

It works with dynamically created fields. If the form is hard-coded, replace it this.valuewith the field index.

+1
source

you can try the following code: $ ("Input.billingAddress"). (On "change", function () {

            var val1 = $("input.street").val();
            var val2 = $("input.suitNumber").val();
            var val3 = $("input.city").val();
            if(this.checked)
            {
                $("input.streetb").val(val1);
                $("input.suitNumberb").val(val2);
                $("input.cityb").val(val3);
            }

            else
            {
                $("input.streetb").val("");
                $("input.suitNumberb").val("");
                $("input.cityb").val("");
            }

            });
0
source

All Articles