我在 javascript 中有一个变量“count”,还有一个我想依赖于该变量的单选按钮。有一个按钮可以生成更多单选按钮,这就是为什么我需要它们的名称属性不同。
我的代码:
var count = 1;
function newForm(){
...<input name=count type="radio" value="Website" />...
}
但它只是将每个附加单选按钮的名称设置为“count”,而不是“count”代表的数字。
完整代码如下:
var count = 1;
function newForm(){
var newdiv = document.createElement('div');
newdiv.innerHTML = '<div class="line"></div><br><input type="text" name="Name"
class="field" placeholder="Full Event Name" /><br><input type="text" name="Location"
placeholder="Event Location" class="field" /><br> <input type="text" name="Date"
placeholder="Event Date" class="field" /> <br> <input type="text" name="End"
placeholder="Event End Date (If Applicable)" class="field" /> <br> <input type="text"
name="Time" placeholder="Event Time" class="field" /> <br> <input type="text"
name="Tags"
placeholder="Relevant Tags" class="field" /> <br> The info is from: <input name=count
type="radio" value="Tweet" checked="" />Tweet <input name=count type="radio"
value="Website"
/>Website <input name=count type="radio" value="Tweet and Website" /> Tweet and
Website';
if(count < 10) {
document.getElementById('formSpace').appendChild(newdiv);
count++;
}
顺便说一句,上面的 newdiv.innerHTML 字符串在代码中全部在一行上。
最佳答案
如果您尝试创建元素,请使用 createElement()
:
var count = 1;
function newForm(){
var input = document.createElement('input');
input.name = count;
input.type = 'radio';
input.value = 'Website';
}
关于javascript - 尝试将 html 属性设置为 javascript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16702022/