javascript – 如何在提交时向表单添加新的隐藏输入字段

前端之家收集整理的这篇文章主要介绍了javascript – 如何在提交时向表单添加新的隐藏输入字段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在做一些条件检查,并希望将某些隐藏的变量传递给请求.

例如,如果此条件通过,我想传递这些:@H_404_3@

<script type="text/javascript"> 
   function checkClosureLevel()
    {
        var openLevel = document.getElementById('openLevel');
        var phyCompLevel = document.getElementById('phyCompLevel');
        var finCompLevel = document.getElementById('finCompLevel');

        if(openLevel.checked){ 

//PASS HIDDEN FORM VARIBALES HERE AND SUBMIT FORM   
        }
             }
</script>
<form action="process.det_details" method="post" name="detParameterForm">

   <fieldset class="det">
        <legend>Closure Level</legend>
        <input type="checkBox" name="openLevel" >Open</input><br/>
        <input type="checkBox" name="phyCompLevel" >Physically Complete</input><br/>
        <input type="checkBox" name="finCompLevel" >Financially Complete</input>
    </fieldset>

解决方法

document.forms对象是所有< form>的集合.页面中的元素.它具有数字索引和命名项目.命名项目对应于每个< form>的name属性.
var theForm = document.forms['detParameterForm'];

为了使附加数据更容易,您可以创建一个为给定表单添加数据的函数.@H_404_3@

function addHidden(theForm,key,value) {
    // Create a hidden input element,and append it to the form:
    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = key;'name-as-seen-at-the-server';
    input.value = value;
    theForm.appendChild(input);
}

// Form reference:
var theForm = document.forms['detParameterForm'];

// Add data:
addHidden(theForm,'key-one','value');
addHidden(theForm,'another','meow');
addHidden(theForm,'foobarz','baws');

// Submit the form:
theForm.submit();
原文链接:https://www.f2er.com/js/154531.html

猜你在找的JavaScript相关文章