日安。
我需要将输入到文本框中的内容复制到多个其他文本框。
问题是,有一个下拉列表,它必须确定这一点。
处理:用户将在“商品”文本框中输入“商品”。
在此之后,他们将从下拉列表中选择“投递地点”的数量。如果他们选择"1";文本框"Commodity1“将具有与主”商品“文本框相同的内容。
如果他们选择"2“作为"DeliveryPlaces",则"Commodity1”和"Commodity2“的内容都将输入到”商品“中。这个过程应该一直延续到"DeliveryPlaces“的"5”为止。
我希望这样做的原因是,我们的大多数客户只会订购一种商品,即使它将被运送到多个地点;
function copy(){
if (document.getElementById("DeliveryPlaces").value = "1")
{
document.getElementById("Commodity1").value=document.getElementById("Commodity").value;
}
else if (document.getElementById("DeliveryPlaces").value = "2")
{
document.getElementById("Commodity1").value=document.getElementById("Commodity").value;
document.getElementById("Commodity2").value=document.getElementById("Commodity").value;
}
else if (document.getElementById("DeliveryPlaces").value = "3")
{
document.getElementById("Commodity1").value=document.getElementById("Commodity").value;
document.getElementById("Commodity2").value=document.getElementById("Commodity").value;
document.getElementById("Commodity3").value=document.getElementById("Commodity").value;
}
else if (document.getElementById("DeliveryPlaces").value = "4")
{
document.getElementById("Commodity1").value=document.getElementById("Commodity").value;
document.getElementById("Commodity2").value=document.getElementById("Commodity").value;
document.getElementById("Commodity3").value=document.getElementById("Commodity").value;
document.getElementById("Commodity4").value=document.getElementById("Commodity").value;
}
else if (document.getElementById("DeliveryPlaces").value = "5")
{
document.getElementById("Commodity1").value=document.getElementById("Commodity").value;
document.getElementById("Commodity2").value=document.getElementById("Commodity").value;
document.getElementById("Commodity3").value=document.getElementById("Commodity").value;
document.getElementById("Commodity4").value=document.getElementById("Commodity").value;
document.getElementById("Commodity5").value=document.getElementById("Commodity").value;
}
else
{
document.getElementById("Commodity1").value="";
document.getElementById("Commodity2").value="";
document.getElementById("Commodity3").value="";
document.getElementById("Commodity4").value="";
document.getElementById("Commodity5").value="";
}
}<p>
Commodity
</p><input type="textbox" id="Commodity" onkeyinput="copy();">
<p>
Commodity1
</p><input type="textbox" id="Commodity1">
<p>
Commodity2
</p><input type="textbox" id="Commodity2">
<p>
Commodity3
</p><input type="textbox" id="Commodity3">
<p>
Commodity4
</p><input type="textbox" id="Commodity4">
<p>
Commodity5
</p><input type="textbox" id="Commodity5">
<p>
Delivery Places
</p>
<select style="width:50px;" id="DeliveryPlaces">
<option value="-">-</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
因此,通过自动填写它,我们可以节省用户的时间。
我设法使它在没有条件的情况下工作,这很简单。然而,现在,它只是填写"Commodity1“,即使我选择了一个"DeliveryPlace”而不是"1“。
任何帮助都将不胜感激。
发布于 2017-01-04 14:34:02
开关:
if (document.getElementById("DeliveryPlaces").value = "1")
转到if (document.getElementById("DeliveryPlaces").value == "1")
代码实际上是将语句中的DeliveryPlaces切换为1。
发布于 2017-01-04 14:26:57
试着使用:
var e = document.getElementById("DeliveryPlaces")
var selection = e.options[e.selectedIndex].value;然后根据选择中的值来执行您的条件。
您目前正在选择selected的值,而不是实际选择的选项。
编辑:它可能是文本,而不是你需要的价值在你的选择,但给它一个尝试!
发布于 2017-01-04 15:16:32
我会这样做:
function copy() {
var e = document.getElementById("DeliveryPlaces")
var selection = parseInt(e.options[e.selectedIndex].value);
var input = document.getElementById("Commodity").value;
var field;
for (var i = 1; i < 6; i++) {
field = "Commodity" + i;
if (i <= selection) {
document.getElementById(field).value = input;
} else {
document.getElementById(field).value = "";
}
}
}这样,您可以清除其他商品,当您切换到较低的交付位置+您可以轻松地通过编辑for循环添加更多的商品。
https://stackoverflow.com/questions/41465855
复制相似问题