Javascript 将更多下拉框值附加到文本框区域 函数chkind1(){ var dropdown2=document.getElementById('dropdown1'); var textbox=document.getElementById('textbox1'); textbox.value=dropdown1.value; } 函数chkind2(){ var dropdown3=document.getElementById('dropdown2'); var textbox=document.getElementById('textbox2'); textbox.value=dropdown2.value; }

Javascript 将更多下拉框值附加到文本框区域 函数chkind1(){ var dropdown2=document.getElementById('dropdown1'); var textbox=document.getElementById('textbox1'); textbox.value=dropdown1.value; } 函数chkind2(){ var dropdown3=document.getElementById('dropdown2'); var textbox=document.getElementById('textbox2'); textbox.value=dropdown2.value; },javascript,html,Javascript,Html,与其重写以前的文本框选择,不如添加以下内容: <html> <script type="text/javascript"> function chkind1(){ var dropdown2 = document.getElementById('dropdown1'); var textbox = document.getElementById('textbox1'); textbox.value=dropdown1.value;

与其重写以前的文本框选择,不如添加以下内容:

<html>
<script type="text/javascript">
    function chkind1(){
    var dropdown2 = document.getElementById('dropdown1');
    var textbox = document.getElementById('textbox1');
    textbox.value=dropdown1.value;
    }
    </script>


<script type="text/javascript">
    function chkind2(){
    var dropdown3 = document.getElementById('dropdown2');
    var textbox = document.getElementById('textbox2');
    textbox.value=dropdown2.value;
    }
    </script>

<input id="textbox1" name="size" type="text" />
<input id="textbox2" name="copies" type="text" />

<select onchange=chkind1()' id='dropdown1'>
<option value='Nil'>Select Print Size</option>
<option value = '$file - 5x7'>5x7</option>
<option value = '$file - 6x8'>6x8</option>
</select>

<select onchange='chkind2()' id='dropdown2'>
<option value = 'Nil'>Select how many Copies</option>
<option value = '1'>1</option>
<option value = '2'>2</option>
</select>

</html>

更改值分配以将所选内容附加到当前值:

textbox.value += ", " + dropdown1.value;
在引号之间添加任何字符以分隔条目

更新:

如果再次选择该值,则根据注释中的每个问题删除该值

textbox.value += ' ' + dropdown1.value;

检查字符串中是否包含该值<如果值存在,则code>indexOf返回-1。然后将该值指定给已删除该值的字符串。

工作正常,谢谢!忘记询问是否存在,以便如果选择相同的值,它会将其从文本框中删除?
if(textbox.value.indexOf(dropdown1.value) == -1) {
    textbox.value = textbox.value.replace(dropdown1.value, '');
} else {
    textbox.value += ' ' + dropdown1.value;
}