[英]dynamic opened dropdown menu
假设用户输入一个值,例如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>
我知道应该使用循环,但是我不知道...
根据我的猜测,您希望输入最后一个数字来确定所选内容所具有的选项数量,而不删除先前的选项。 如果不是这种情况,请根据您的情况调整此代码段,但您应该大致了解如何向select添加元素。
这是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]);
}
}
}
如果这不是您想要的,那么您将让我更加具体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.