我一直试图解决这个问题。我想在表单内的指定段落标记内创建隐藏的输入标记。我在数组中有一个变量输入名称和值。我想要发生的是在处理表单之前,在单击提交按钮后,将隐藏的输入元素附加自动增量。这就是我所做的,这显然是不正确的:
<script type="text/javascript">
function insertInput(){
hname=["name1","name2","name3","name4"];
hvalue=["value1","value2","value3","value4"];
var i=0;
for (;hname[i];){
var para, hiddenInput, br;
para = document.getElementById('hidden');
hiddenInput = document.createElement('input');
hiddenInput.type = 'hidden';
hiddenInput.name = hname[i];
hiddenInput.value = hvalue[i];
para.appendChild(hiddenInput);
br = document.createElement('br');
para.appendChild(br);
return false;
i++;
}
</script>
<form id="form1">
<p><label>Username:</label> <input type="text" name="username" size="10" /></p>
<p><label>Password:</label> <input type="password" name="password" size="10"/></p>
<p id="hidden">
<!-- Insert Hidden input tags tag here -->
</p>
<button type="submit" onclick="return insertInput();">Log In</button>
</form>
这是我点击提交按钮后想要实现的目标:
<form id="form1">
<p><label>Username:</label> <input type="text" name="username" size="10" /></p>
<p><label>Password:</label> <input type="password" name="password" size="10"/></p>
<p id="hidden">
<!-- Insert Hidden input tags tag here -->
<input type="hidden" name="name1" value="value1"/><br/>
<input type="hidden" name="name2" value="value2"/><br/>
<input type="hidden" name="name3" value="value3"/><br/>
<input type="hidden" name="name4" value="value4"/><br/>
</p>
<button type="submit" onclick="return insertInput();">Log In</button>
</form>
请帮忙!先谢谢你了!
答案 0 :(得分:2)
您的代码中存在多个问题。
}
<br />
来分隔隐藏字段。var
关键字的变量,它将导致创建一个全局变量而不是局部变量。i++
可以放入post语句(for loop)的JavaScript
<script tpe="text/javascript">
function insertInput(){
var hname=["name1","name2","name3","name4"];
var hvalue=["value1","value2","value3","value4"];
var i=0;
for (;hname[i]; i++){
var para, hiddenInput, br;
para = document.getElementById('hidden');
hiddenInput = document.createElement('input');
hiddenInput.type = 'hidden';
hiddenInput.name = hname[i];
hiddenInput.value = hvalue[i];
para.appendChild(hiddenInput);
}
return false;
}
</script>