按下提交按钮时,我正在尝试创建下拉列表。这将通过一个while循环来制作不同数量的列表。我目前有以下代码,但它没有按预期工作。
<html>
<head>
<script type="text/javascript">
function getvalue() {
number = document.getnumber.input.value;
document.getElementById("result").value = number;
}
</script>
</head>
<body>
<script>
function generatedropdown() {
while (i < number) {
html = '<select name="select" id="i">';
html += '<option>Test 1</option>';
html += '<option>Test 2</option>';
html += '<option>Test 3</option>';
html += '<option>Test 4</option>';
html += '<option>Test 5</option>';
html += '</select>';
innerHTML = html;
i++;
}
}
</script>
<form name="getnumber">Input number:
<input type="text" name="input">
<input type="button" value="Next" onClick="getvalue()">
</form>
<form id="showlists">Number entered:
<input type="text" id="result" readonly="readonly">
<input type="button" value="Show lists" onClick="generatedropdown()">
</form>
</body>
</html>
编辑:我已将代码更改为此,但仍然没有运气。
<html>
<head>
<script type="text/javascript">
function getvalue() {
number = document.getnumber.input.value;
document.getElementById("result").value = number;
}
</script>
</head>
<body>
<script>
function generatedropdown() {
html = '<select name="select" id="i">';
while (i < number) {
html+='<option>Test 1</option>';
html+='<option>Test 2</option>';
html+='<option>Test 3</option>';
html+='<option>Test 4</option>';
html+='<option>Test 5</option>';
i++;
}
html+='</select>';
document.getElementById("my-output").innerHTML = html;
}
</script>
<form name="getnumber">
Input number: <input type="text" name="input">
<input type="button" value="Next" onClick="getvalue()">
</form>
<form id="showlists">
Number entered: <input type="text" id="result" readonly="readonly">
<input type="button" value="Show lists" onClick="generatedropdown()">
<div id="my-output">Generated List:</div>
</form>
</body>
</html>