jQuery-为什么不选择框信息复制,但其余的将选择
我正在使用下面的代码尝试在选中复选框时将信息从一组地址字段复制到另一组。除“状态选择框”下拉框外,其他所有功能均有效。这快把我逼疯了jQuery-为什么不选择框信息复制,但其余的将选择,jquery,Jquery,我正在使用下面的代码尝试在选中复选框时将信息从一组地址字段复制到另一组。除“状态选择框”下拉框外,其他所有功能均有效。这快把我逼疯了 //on page load $(document).ready(function() { //when the checkbox is checked or unchecked $('#copyaddress').click(function() { // If checked if ($(this).is(":checked")) {
//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');
}
});
});
设置单个值选择元素的值时,只需传递要选择的选项的文本值 这可以修改
//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());