简体   繁体   English

提交时如何使用 JavaScript 为 Forms 添加更多输入标签值

[英]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.createElementappendChild

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.

相关问题 如果输入标签旁边有更多按钮,当按下“Enter”键时,如何在 JavaScript 中获取 input.value? - How to get input.value in JavaScript when the “Enter” key is pressed if I have more buttons next to the input tag? 提交表单并使用值(JavaScript) - Submitting forms and using the value (JavaScript) 未选择值时如何防止按钮提交表单。 不使用JavaScript - How to prevent buttons from submitting forms when value is not selected. Without using javascript 将 HTML 表单提交到 Google 表格时如何添加更多列 - How to add more Columns when Submitting a HTML form to Google Sheets 向隐藏的输入标签添加值并提交到表单 - Adding value to hidden input tag and submitting to form 如何在使用javascript提交之前更改隐藏的输入值? - How to change a hidden input value just before submitting using javascript? 如何在提交到数组 Javascript 之前将 input.value 更改为大写 - How to change input.value to capitalize before submitting to array Javascript 按下逗号时如何在输入标签中添加更多图标 - How can I add more icon in input tag when I key down comma 从中获取src值的图像 <img> 标记并提交给 <input type=“file”> 通过JavaScript - Getting an image from value of src in <img> tag and submitting it to <input type=“file”> via javascript 使用 Javascript 单击锚标记时,如何更改输入标记的值、名称和占位符? - How to change value, name and placeholder of an input tag when an anchor tag is clicked using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM