2011-04-18 16 views
0

我使用下面的代碼來嘗試複製信息從一組地址字段到另一組時複選框被選中。除了狀態選擇框(下拉框)之外,它全部工作。這讓我瘋狂。jQuery - 爲什麼不選擇框信息重複,但其餘部分將

//on page load 
$(document).ready(function() { 

//when the checkbox is checked or unchecked 
$('#copyaddress').click(function() { 

    // If checked 
    if ($(this).is(":checked")) { 

     //for each input field 
     $('#cc input', ':visible', document.body).each(function(i) { 
      //copy the values from the billing_fields inputs 
      //to the equiv inputs on the shipping_fields 
      $(this).val($('#info input').eq(i).val()); 
     }); 
     //won't work on drop downs, so get those values 
     var c_state = $("select#c_state").val(); 

     // special for the select 
     $('select#cc_state option[value=' + c_state + ']').attr('selected', 'selected'); 

    } else { 

     //for each input field 
     $('#cc input', ':visible', document.body).each(function(i) { 
      // put default values back 
      $(this).val($(this)[0].defaultValue); 
     }); 

     // special for the select 
     $('select#cc_state option[value=""]').attr('selected', 'selected'); 
     } 
    }); 
}); 

回答

1

設置單個值select元素的值時,只需傳遞要選擇的選項的文本值即可。 Check out the jQuery Documentation here on .val(value)

這可以被修改......

//won't work on drop downs, so get those values 
    var c_state = $("select#c_state").val(); 

    // special for the select 
    $('select#cc_state option[value=' + c_state + ']').attr('selected', 'selected'); 

這個

$('select#cc_state').val($("select#c_state").val()); 
+0

感謝那些真正變薄下來。現在正在工作。 – Vincent 2011-04-18 21:02:35

相關問題