根据我的猜测,您希望输入最后一个数字来确定选择的选项数量,而不删除以前的选项。如果不是这种情况,请根据您的情况调整此代码段,但您应该大致了解如何将元素添加到选择中。
这是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]);
}
}
}
如果这不是您想要的,那么您将更具体地成为我。