net*_*911 3 javascript forms input function
我有隐藏输入标签的变量名称和值,我想在单击提交按钮时将其附加到表单中。我该如何编码?
这是我的代码:
<script type="text/javascript">
hname="reference";
hvalue="1";
function insertInput(){
document.write( "<input type='hidden' name='" + hname + " ' value=' " + hvalue + " '/><br/>");
}
</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 tag here --></p>
<button type="submit' onClick="insertInput();">Log In</button>
</form>
Run Code Online (Sandbox Code Playgroud)
我似乎无法让它工作。
尝试这个:
<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 tag here --></p>
<button type="submit" onclick="return insertInput();">Log In</button>
</form>
<script type="text/javascript">
hname="reference";
hvalue="1";
function insertInput(){
var para, hiddenInput, br;
para = document.getElementById('hidden');
hiddenInput = document.createElement('input');
hiddenInput.type = 'hidden';
hiddenInput.name = hname;
hiddenInput.value = hvalue;
para.appendChild(hiddenInput);
br = document.createElement('br'); //Not sure why you needed this <br> tag but here it is
para.appendChild(br);
return false; //Have this function return true if you want to post right away after adding the hidden value, otherwise leave it to false
}
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19760 次 |
| 最近记录: |