Javascript - 用 1 个选择列表填充 2 个文本框

Javascript - Populate 2 textbox with 1 select list

本文关键字:填充 文本 列表 选择 Javascript      更新时间:2023-09-26

在下拉框中选择某些内容时,我需要填充 2 个不同的文本框。

从小提琴上,我可以用一个来做。

对不起,让我再澄清一点。此下拉列表实际上是此人的长名称。我正在尝试在选择他的名字时填充"昵称"和"联系电话"文本框。

看看这个演示,我现在有 2 个文本框。在我的数据库中,我有每个人的全名、昵称和联系电话的记录。由我决定如何创建选择列表,但我只想选择他的全名以在 2 个单独的框中显示昵称和联系电话。

抱歉,您的示例只有一个 texarea...

但是,如果我没有误解您的问题,您可以简单地这样做:

var mytextbox = document.getElementById('mytext');
var mytextbox2 = document.getElementById('mytext2');
var mydropdown = document.getElementById('dropdown');
mydropdown.onchange = function(){
   mytextbox.value = mytextbox.value  + this.value;
   mytextbox2.value = mytextbox2.value  + this.value;
}
<textarea id="mytext"></textarea>
<textarea id="mytext2"></textarea>
<select id="dropdown">
<option value="">None</option>
<option value="contactnumber1">text1</option>
<option value="contactnumber2">text2</option>
<option value="contactnumber3">text3</option>
<option value="contactnumber4">text4</option>
</select>
var mytextbox = document.getElementById('mytext');
var mytextbox2 = document.getElementById('mytext2');
var mydropdown = document.getElementById('dropdown');
mydropdown.onchange = function () {
mytextbox.value = mydropdown.options[this.selectedIndex].text;
mytextbox2.value = mydropdown.options[this.selectedIndex].value;
};

你的意思是,你想在每次某个身体从下拉列表中选择项目时附加选择?我在你的小提琴中没有看到两个文本框,这就是我问的原因......

如果您希望选择唯一值,则..

var mytextbox = document.getElementById('mytext');
var mydropdown = document.getElementById('dropdown');
var strValue =  new String();
mydropdown.onchange = function(){
if(strValue.indexOf(this.value)<=0){
   if(strValue!="")
{
    strValue += "," + this.value;
}
else
{
    strValue = this.value
}
}
mytextbox.value = strValue;
}

因此,您希望用一个标签填充两个值。 话虽如此,如果您只是将数据属性放在选项标签中怎么办? 数据联系人号码 数据长名

<select>  
<option value="x" data-contactNumber = "yyyy" data-longName="zzzz">  
.....
</select>  

然后,您可以只为所选选项文本框分配单个数据属性??