这是javascript代码,其中我想通过javascript中的循环打印9次我的选择输入字段
<script>
var text = "";
var i;
for (i = 0; i < 9 i++) {
text += "<select name="2" style="color:#000000; background-color:#CCCCCC ">
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>"
"<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
<p id="demo"> </p>
请告诉我最好的方法
答案 0 :(得分:2)
尝试正确连接HTML字符串,
for (i = 0; i < 9; i++) {
text += "<select name='2' style='color: #000000; background-color:# CCCCCC;'>" +
"<option>3</option>" +
"<option>4</option>" +
"<option>5</option>" +
"<option>6</option>" +
"</select>" +
"<br>";
}
document.getElementById("demo").innerHTML = text;
编辑:当选项标记的值和文本相同时,无需显式设置值属性 - Mark Erikkson
Uff ..好的。
<!DOCTYPE html>
<html>
<body>
<div id="demo"></div>
<script>
var text = "";
for (i = 0; i < 9; i++) {
text += "<select name='2' style='color: #000000; background-color:# CCCCCC;'>" +
"<option value='3'>3</option>" +
"<option value='4'>4</option>" +
"<option value='5'>5</option>" +
"<option value='6'>6</option>" +
"</select>" +
"<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>