2013-07-16 142 views
1

假设用户输入一个值为例如5,然后打开新的下拉菜单为5.当用户更新值为7时,添加新的两个下拉菜单,但前5个不会删除。我如何做到这一点?动态打开的下拉菜单

它是关于使用形式

function add(text){ 
var TheTextBox = document.forms[0].elements['field_name']; //I think that's right, haven't done it in a while 
TheTextBox.value = TheTextBox.value + text; 

}

<select onchange="optionChanged2(this);"> 
    <option value="0">Hayır</option> 
    <option value="1">Evet</option> 
</select> 

添加文本框,我知道应该循环使用,但我怎么不知道......基于

+2

请问你可以把这个[jsfiddle](http://jsfiddle.net/)吗? – Laszlo

+0

您希望根据在文本框中输入的添加数量添加新选项到下拉列表中,而不删除以前的选项? –

+0

5用户输入7后再添加2个菜单吗? –

回答

0

我猜测,你想要输入最后一个数字来确定选择的数量,而不删除以前的数字。如果情况并非如此,请根据您的情况修改此片段,但您应该了解如何将元素添加到选区。

这里是HTML的一部分:

<input type="text" id="text"> 
<select id="sel"> 
    <option value="0">Option 0</option> 
    <option value="1">Option 1</option> 
</select> 

和JavaScript部分:

function add(text){ 
    var TheTextBox = document.getElementById("text"); 
    var TheSelect = document.getElementById("sel"); 

    var num = new Number(TheTextBox.value); // Making sure we get a number out of it 
    if(num > TheSelect.options.length){ // Do we have a number greater than the number of options 
     for(var i=0; i<(num-TheSelect.options.length); ++i){ // Add the extra options 
      var option = document.createElement("option"); 
      option.setAttribute("value", i); 
      option.innerHTML = text; 
      TheSelect.appendChild(option); 
     } 
    } 
    else { // We have more options that we need to lets remove them from the end 
     for(var i=0; i<(TheSelect.options.length-num); ++i){ 
      TheSelect.removeChild(TheSelect.options[TheSelect.options.length-1]); 
     } 
    } 
} 

如果这不是你想要的,你将不得不成为我更具体。