![](/img/trans.png)
[英]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.