[英]How to add more input tag value with JavaScript for Forms when submitting
I have a page which may has about more than 1 form like below.我有一个页面,其中可能有大约 1 个以上的表单,如下所示。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page</title>
</head>
<body>
<form id="form1">
<input type="text" name="fname1" />
<input type="submit" />
</form>
<form id="form2">
<input type="text" name="fname2"/>
<input type="submit" />
</form>
<form id="form3">
<input type="text" name="fname3" />
<input type="submit" />
</form>
<script>
var formsx = document.forms.length;
for (i= 0; i < formsx; i++) {
form = document.forms[i];
form.addEventListener("submit", function(evt){
/// alert("form action");
});
}
</script>
</body>
</html>
I would like to add one hidden value () whenever user submits form with Classic Javascript.每当用户使用经典 Javascript 提交表单时,我想添加一个隐藏值 ()。 If any of form submit, the new hidden value should be added.
如果有任何表单提交,则应添加新的隐藏值。 So at the backend, the POST request will have two "name" and "hidden" parameters values instead of one according to above script.
所以在后端,POST 请求将有两个“名称”和“隐藏”参数值,而不是根据上面的脚本一个。 I just want to code with JavaScript only.
我只想用 JavaScript 编码。 Please let me know how can I do that, thanks in advance.
请让我知道我该怎么做,在此先感谢。
You just need to use document.createElement
and appendChild
:您只需要使用
document.createElement
和appendChild
:
form.addEventListener("submit", function(evt){
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", newName);
input.setAttribute("value", "value");
form.appendChild(input);
return true;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.