简体   繁体   English

将更多下拉框值附加到文本框区域

[英]Append more drop down box value to text box area

<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>

Hi all, trying to achieve on the first drop down box (size), is not to overwrite each selection but to append or add each selection one after the other. 大家好,尝试在第一个下拉框(大小)上实现的目的不是覆盖每个选择,而是一个接一个地追加或添加每个选择。 EG 5x7, 6x8, etc etc. EG 5x7、6x8等

I have had a go but can't seem to get it right, could I please have some help on this. 我试过了,但似乎做得不好,请给我一点帮助。 Cheers. 干杯。

Change your value assignment to append your selection to the current value: 更改值分配,以将选择内容附加到当前值:

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

Add whatever characters in between the quotes to separate your entries. 在引号之间添加任何字符以分隔条目。

UPDATE: 更新:

Per question in the comment to remove the value if it is selected again. 注释中的每个问题都会删除该值(如果再次选择该值)。

if(textbox.value.indexOf(dropdown1.value) == -1) {
    textbox.value = textbox.value.replace(dropdown1.value, '');
} else {
    textbox.value += ' ' + dropdown1.value;
}

Check to see if the value is contained in the string. 检查该值是否包含在字符串中。 indexOf returns -1 if the value is note present. 如果存在注释,则indexOf返回-1。 Then you assign the value to be the string with that value removed. 然后,将值分配为已删除该值的字符串。

与其覆盖以前的文本框选择,不如添加它:

textbox.value += ", " + dropdown1.value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM