How to copy the shipping address to billing address

自闭症网瘾萝莉.ら 提交于 2019-12-23 17:30:13

问题


I like to know if I can copy the shipping address to billing address. When a user clicks same as shipping address checkbox. The shipping address value will be copied to billing input field. I got most of the parts done but I am not sure how to copy select menu (states) value to billing address. I really appreciate any helps.

My code

$(document).ready(function(){

Jquery

$('#same').click(function(){
    if($('#same').attr('checked')){
        $('#bfName').val($('#fName').val());
        $('#blName').val($('#lName').val());
        $('#baddress1').val($('#address1').val());
        $('#baddress2').val($('#address2').val());
        $('#bcity').val($('#city').val());
        alert(($('#state option:selected').val())); //not sure what to do here
        $('#bzip').val($('#zip').val());

};

});

Html

<td><select name="state">            //shipping states......only partial codes.
    <option value="">None
    <option value="AL">Alabama
    <option value="AK">Alaska
    <option value="AZ">Arizona
    <option value="AR">Arkansas
    <option value="CA">California
    <option value="CO">Colorado
    <option value="CT">Connecticut
        </select></td>


   <td><select name="bstate">    //billing state................only partial codes.
    <option value="">None
    <option value="AL">Alabama
    <option value="AK">Alaska
    <option value="AZ">Arizona
    <option value="AR">Arkansas
    <option value="CA">California
    <option value="CO">Colorado
    <option value="CT">Connecticut
        </select></td>

Thanks a lot!


回答1:


Give this a try.

var state = $('#state option:selected').val();
$('#bstate option[value=' + state + ']').attr('selected','selected');



回答2:


does this work ?

  $('#bstate').val($('#state').val());

you may need to add id attribute to that billing and shipping state select as 'astate' and 'bstate', as i can't seems to find one :)




回答3:


// make billing same as address
    $('input[name=same]').click(function() {
    //alert('Using the same address');  
    if ($("input[name=same]:checked").is(':checked')) { 
      $('#account_bill_fname').val($('#account_fname').val());
      $('#account_bill_lname').val($('#account_lname').val());
      $('#account_bill_address1').val($('#account_address1').val());
      $('#account_bill_address2').val($('#account_address2').val());
      $('#account_bill_city').val($('#account_city').val());             
      var state = $('select[name=account_state] option:selected').val(); 
      $('select[name=account_bill_state] option[value=' + state + ']').attr('selected','selected');
      var country = $('select[name=account_country] option:selected').val();                            
      $('select[name=account_bill_country] option[value=' + country + ']').attr('selected','selected');      
      $('#account_bill_postal').val($('#account_postal').val());
      };              
    });

on the html page the state and country are dropdowns and the checkbox is:

<input type="checkbox" name="same" value="Y" />


来源:https://stackoverflow.com/questions/2706756/how-to-copy-the-shipping-address-to-billing-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!