首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将文本从文本框中的键输入复制到其他基于下拉列表的文本框?

将文本从文本框中的键输入复制到其他基于下拉列表的文本框?
EN

Stack Overflow用户
提问于 2017-01-04 14:11:37
回答 4查看 123关注 0票数 0

日安。

我需要将输入到文本框中的内容复制到多个其他文本框。

问题是,有一个下拉列表,它必须确定这一点。

处理:用户将在“商品”文本框中输入“商品”。

在此之后,他们将从下拉列表中选择“投递地点”的数量。如果他们选择"1";文本框"Commodity1“将具有与主”商品“文本框相同的内容。

如果他们选择"2“作为"DeliveryPlaces",则"Commodity1”和"Commodity2“的内容都将输入到”商品“中。这个过程应该一直延续到"DeliveryPlaces“的"5”为止。

我希望这样做的原因是,我们的大多数客户只会订购一种商品,即使它将被运送到多个地点;

代码语言:javascript
复制
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="";

  }
}
代码语言:javascript
复制
<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“。

任何帮助都将不胜感激。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-01-04 14:34:02

开关:

if (document.getElementById("DeliveryPlaces").value = "1")

转到if (document.getElementById("DeliveryPlaces").value == "1")

代码实际上是将语句中的DeliveryPlaces切换为1。

票数 2
EN

Stack Overflow用户

发布于 2017-01-04 14:26:57

试着使用:

代码语言:javascript
复制
var e = document.getElementById("DeliveryPlaces")
var selection = e.options[e.selectedIndex].value;

然后根据选择中的值来执行您的条件。

您目前正在选择selected的值,而不是实际选择的选项。

编辑:它可能是文本,而不是你需要的价值在你的选择,但给它一个尝试!

票数 2
EN

Stack Overflow用户

发布于 2017-01-04 15:16:32

我会这样做:

代码语言:javascript
复制
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循环添加更多的商品。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41465855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档