我一直在研究如何将文本字段值复制到下拉菜单中,并且只看到如何完成相反的操作。有人可以帮我调整这段代码,以便将运费城市价值(文本字段)复制到结算城市(下拉菜单)吗?
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<P>
<script type="text/javascript">
<!--
function FillBilling(f) {
if(f.billingtoo.checked == true) {
f.billingname.value = f.shippingname.value;
f.billingcity.value = f.shippingcity.value;
}
}
// -->
</script>
<TABLE BORDER=1><TR><TD BGCOLOR="eeeeee">
<b>Mailing Address</b>
<br><br>
<form>
Name:
<input type="text" name="shippingname">
<br>
City:
<input type="text" name="shippingcity">
<br>
<input type="checkbox" name="billingtoo" onclick="FillBilling(this.form)">
<em>Check this box if Billing Address and Mailing Address are the same.</em>
<P>
<b>Billing Address</b>
<br><br>
Name:
<input type="text" name="billingname">
<br>
City:
<select name="billingcity">
<option value="City 1">City 1</option>
<option value="City 2">City 2</option>
<option value="City 3">City 3</option>
</select>
</form>
</TD></TR></TABLE>
</body>
</html>
答案 0 :(得分:0)
您需要有效地使用ID和document.getElementById以获取正确的表单元素,并在单击按钮时在dropbox / select中重新分配值。
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<P>
<script type="text/javascript">
<!--
function FillBilling(f) {
if(f.billingtoo.checked == true) {
document.getElementById("billingname").value = document.getElementById("shippingname").value;
var optn = document.createElement("OPTION");
optn.text = document.getElementById("shippingcity").value;;
optn.value = document.getElementById("shippingcity").value;;;
document.getElementById("billingcity").options.add(optn);
}
}
// -->
</script>
<TABLE BORDER=1><TR><TD BGCOLOR="eeeeee">
<b>Mailing Address</b>
<br><br>
<form>
Name:
<input type="text" name="shippingname" id="shippingname">
<br>
City:
<input type="text" name="shippingcity" id="shippingcity">
<br>
<input type="checkbox" name="billingtoo" onclick="FillBilling(this.form)">
<em>Check this box if Billing Address and Mailing Address are the same.</em>
<P>
<b>Billing Address</b>
<br><br>
Name:
<input type="text" name="billingname" id="billingname">
<br>
City:
<select id="billingcity" name="billingcity">
<option value="City 1">City 1</option>
<option value="City 2">City 2</option>
<option value="City 3">City 3</option>
</select>
</form>
</TD></TR></TABLE>
</body>
</html>
为了更改选择值而不是添加,您可以使用:
document.getElementById("billingcity").value=document.getElementById("shippingcity").value;
答案 1 :(得分:0)
要向select元素添加值,需要添加具有适当text和value属性的option元素。所以你可以这样做:
var select = f.billingcity;
// Create a new option element, passing values for the text and value proprties
var option = new Option(f.shippingcity.value, f.shippingcity.value);
// Append the option to the select
select.appendChild(option);
// Make the new option the selected option
option.selected = true;
答案 2 :(得分:0)
我建议包括jquery 然后做这样的事情:
function FillBilling(f) {
if(f.billingtoo.checked == true) {
var $firstCity = $('select option:first-child');
$firstCity.attr('text', f.shippingcity.value);
$firstCity.attr('value', f.shippingcity.value);
}
这应该更新第一个选择城市用户输入
的选项另外,如另一位用户建议更好的做法是给select一些id(例如selectIdentificator),然后像这样更新上面的代码
var $firstCity = $('#selectIdentificator option:first-child');
答案 3 :(得分:0)
这有点乱,但这是我对你似乎想要的尝试:
function FillBilling(f) {
if (f.billingtoo.checked === true && f.shippingcity.value) {
f.billingname.value = f.shippingname.value;
var found = false;
for (var i = 0; i < f.billingcity.options.length; i++) {
if (f.billingcity.options[i].value.toLowerCase() === f.shippingcity.value.toLowerCase()) {
f.billingcity.selectedIndex = i;
found = true;
break;
}
}
if (!found) {
var extraOption = f.billingcity.getAttribute("data-extra-option");
if (extraOption) {
f.billingcity.options[f.billingcity.options.length - 1].text = f.shippingcity.value;
f.billingcity.options[f.billingcity.options.length - 1].value = f.shippingcity.value;
} else {
var newOption = new Option(f.shippingcity.value, f.shippingcity.value);
f.billingcity.setAttribute("data-extra-option", "true");
f.billingcity.appendChild(newOption);
f.billingcity.selectedIndex = f.billingcity.options.length - 1;
}
} else {
if (f.billingcity.getAttribute("data-extra-option")) {
f.billingcity.removeChild(f.billingcity.options[f.billingcity.options.length - 1]);
f.billingcity.selectedIndex = 0;
}
}
} else {
if (f.billingcity.getAttribute("data-extra-option")) {
f.billingcity.removeChild(f.billingcity.options[f.billingcity.options.length - 1]);
f.billingcity.selectedIndex = 0;
}
}
}
DEMO: http://jsfiddle.net/J8YrU/1/
现在,对于单击的复选框和正在更改的Shipping City文本框值,都会调用此函数。
这个功能的作用是:
所以测试一下,试试这些:
您可以在尝试每件事后检查下拉列表的状态,看看它发生了什么。此外,您还可以在任何位置切换复选框以进行测试。