我一直在试图弄清楚这一点。我想在表单内的指定段落标签内创建隐藏的输入标签。我在数组中有一个变量输入名称和值。我想要发生的事情是在处理表单之前,在单击提交按钮后自动增加隐藏的输入元素。这是我所做的,这显然是不正确的:
<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>
请帮忙!提前谢谢你!