[英]How to get input.value in JavaScript when the “Enter” key is pressed if I have more buttons next to the input tag?
[英]How to add more input tag value with JavaScript for Forms when submitting
我有一个页面,其中可能有大约 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>
每当用户使用经典 Javascript 提交表单时,我想添加一个隐藏值 ()。 如果有任何表单提交,则应添加新的隐藏值。 所以在后端,POST 请求将有两个“名称”和“隐藏”参数值,而不是根据上面的脚本一个。 我只想用 JavaScript 编码。 请让我知道我该怎么做,在此先感谢。
您只需要使用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.