gpt4 book ai didi

javascript - 如何在javascript函数中使用javascript变量作为html输入名称?

转载 作者:行者123 更新时间:2023-11-30 16:22:41 25 4
gpt4 key购买 nike

 function f1(){
document.getElementById("comment_show").innerHTML
="<form action='c.php' method='post'>a:"+
"<input type='text' name='d1'> <br>"+
"<textarea name='comment' rows='5' cols='100'> </textarea> <br>"+

"b:"+
"<input type='text' name='d2'> <br>"+
"<textarea name='comment' rows='5' cols='100'> </textarea> <br>"+

"c:"+
"<input type='text' name='d3'> <br>"+
"<textarea name='comment' rows='5' cols='100'> </textarea> <br>"+

"<input type='submit' name='s1' value='s1'> <br><br><br><br>"+

"</form>";
}

如何用 javascript 变量替换 d1,d2,d3 以使其成为一个循环?

最佳答案

以下是如何自动创建输入:

var inputsCount = 3;

var inputs = "";
var LETTER_START = "a".charCodeAt(0) - 1;
for (var i = 1; i <= inputsCount; i++) {
inputs += String.fromCharCode(LETTER_START + i) + ":"+
"<input type='text' name='d" + i + "'> <br>"+
"<textarea name='comment' rows='5' cols='100'> </textarea> <br>";
}


var inner = "<form action='c.php' method='post'>" +
inputs +
"<input type='submit' name='s1' value='s1'> <br><br><br><br>" +
"</form>";

document.getElementById("comment_show").innerHTML = inner;

关于javascript - 如何在javascript函数中使用javascript变量作为html输入名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34523019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com