簡體   English   中英

如何在提交時向表單添加新的隱藏輸入字段

[英]How to add a new hidden input fields to the form on submit

我希望在滿足某些條件時在請求中傳遞某些隱藏參數。

例如,如果滿足以下條件,我想傳遞這些:

<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 VARIABLES 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>
</form>

document.forms對象是頁面中所有<form>元素的集合。 它有數字索引和命名項。 命名項對應於每個<form>name屬性。

var theForm = document.forms['detParameterForm'];

為了使附加數據更容易,您可以創建一個為給定表單添加數據的函數。

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; // 'the key/name of the attribute/field that is sent to 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM