簡體   English   中英

通過JavaScript獲取表單值

[英]Get form values through JavaScript

這是我的HJTML代碼。 我不知道如何使用JavaScript獲取存儲在filtertime[]值並使它們顯示在屏幕上。

   <form action="index.php" method="post" >

    <div class="col-lg-6"><div class="f-txt-l"><input id="test" type="checkbox" name="filtertime[]" class="morning" value="Morning"></div> <div class="f-txt-r">Morning</div></div>
     <div class="col-lg-6"><div class="f-txt-l"><input  id="test" type="checkbox" name="filtertime[]" class="morning" value="Afternoon"></div> <div class="f-txt-r">Afternoon</div></div>
     <div class="col-lg-6"><div class="f-txt-l"><input  id="test" type="checkbox" name="filtertime[]" class="morning" value="Evening"></div> <div class="f-txt-r">Evening</div></div>
     <div class="col-lg-6"><div class="f-txt-l"><input  id="test" type="checkbox" name="filtertime[]" class="morning" value="Night"></div> <div class="f-txt-r">Night</div></div>

     <div class="col-lg-12"><input type="submit" name="button" class="apply-filter" value="Apply Filter"></div>


        </form>


   <script>
   var new =  document.getElementsById("test").innerhtml
   </script>

如何通過將值存儲在數組中作為filtertime[]來獲取JavaScript輸入值?

嘗試

以你的形式

<form action="index.php" id="myform" method="post" >

在jQuery中

 var datastring = $("#myform").serialize();

JS

var params = '';
for( var i=0; i<document.FormName.elements.length; i++ )
{
   var fieldName = document.FormName.elements[i].name;
   var fieldValue = document.FormName.elements[i].value;

   // use the fields, put them in a array, etc.

   // or, add them to a key-value pair strings, 
   // as in regular POST

   params += fieldName + '=' + fieldValue + '&';
}

這是獲取表單項數組的最簡單方法

 var arrValues = [];
 for (var x =0; x < document.getElementsByClassName("morning").length ; x++)
 {
   arrValues.push(document.getElementsByClassName("morning")[x].checked);
 }

form標簽中添加id

<form action="index.php" id="form_name" method="post" >

使用下面的代碼通過JS獲取所有表單元素:

document.forms["form_name"].getElementsByTagName("input");

注意 :-僅當您的表單中沒有selectstextareas ,以上代碼才有效。

如果您在DOM元素中分配了如下所示的id

<input type="text" name="name" id="uniqueID" value="value" />

然后您可以通過以下代碼訪問它:-

使用Javascript: -

var nameValue = document.getElementById("uniqueID").value;

如果您的表單中有單選按鈕,則使用以下代碼:-

<input type="radio" name="radio_name" value="1" > 1
<input type="radio" name="radio_name" value="0" > 0<br>

使用Javascript: -

var radios = document.getElementsByName('radio_name');

for (var i = 0, length = radios.length; i < length; i++) {
    if (radios[i].checked) {
        // do whatever you want with the checked radio
        alert(radios[i].value);

        // only one radio can be logically checked, don't check the rest
        break;
    }
}

希望它能對您有所幫助:)

為此,最簡單的方法是選擇所有帶有“ morning”類的輸入,然后在foreach中檢查是否選中:

    var item = document.getElementsByClassName("morning"); // get all checkbox
    var checkboxesChecked = []; // result array with ckecked ckeckbox

    for (var i=0; i<item.length; i++) {
         // if is checked add the value into the array
         if (item[i].checked) {
            checkboxesChecked.push(item[i].value);
         }
    }


    console.log(checkboxesChecked);

在“ checkboxesChecked”數組中,您具有復選框的所有值。

暫無
暫無

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

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