简体   繁体   English

如何在div类中检索所有输入值

[英]how to retrieve all the input value in a div class

I tried to retrieve all the input value in this tr class 我试图检索此tr类中的所有输入值

            <tr class="tr-shadow" id="myform">

                     <td>

                        <span class="status--process">
                        <input class="au-input au-input--sm" type="text" name="search" placeholder="Search for datas &amp; reports..." style="width: 90px;" />
                        </span>

                                </td>
                                 <td class="desc"><input class="au-input au-input--sm qty" type="text" name="search" placeholder="i.e. 20 EA" style="width: 100px;" />
                                            </td>
                                             <td class="status--process" >
                                                 <input class="au-input au-input--sm" type="text" name="search" placeholder="shipping cost" style="width: 120px; height: 30px;"  /><br>
                                            <select name="selectSm" id="SelectLm" class="form-control-sm form-control" style="width: 120px;">

                                                   <option value="0">Please select</option>
                                                    <option value="1">Option #1</option>
                                                    <option value="2">Option #2</option>
                                                    <option value="3">Option #3</option>
                                                    <option value="4">Option #4</option>
                                                    <option value="5">Option #5</option>
                                                </select>
                                            </td>
                                                <td>
                                                    <button type="button" class="btn btn-primary btn-md" onclick="postitem()">Submit</button>
                                            </td>
                                        </tr>

I tried using this function 我尝试使用此功能

function postitem(index) {
   var formid='#myform'
    var panel= $(formid);
    var inputs = panel.find("input");
    console.log(inputs.value)

   }

but the text value are not being retrieved. 但未检索到文本值。 What am I doing wrongly and how can I retrieve the text value for all the input? 我在做错什么,我该如何检索所有输入的文本值?

使用它来获取所有值。

$('input').each(function(index){ console.log($(this).val()); });

Use id's for the input fields and then get the value of the element at that id. 在输入字段中使用id,然后获取该id处元素的值。 You can do this like so, using the first input field as an example: 您可以像这样,以第一个输入字段为例:

 function postitem(){ console.log(document.getElementById('first_input').value); } 
  <tr class="tr-shadow" id="myform"> <td> <span class="status--process"> <input class="au-input au-input--sm" id = 'first_input' type="text" name="search" placeholder="Search for datas &amp; reports..." style="width: 90px;" /> </span> </td> <td class="desc"><input class="au-input au-input--sm qty" type="text" name="search" placeholder="ie 20 EA" style="width: 100px;" /> </td> <td class="status--process" > <input class="au-input au-input--sm" type="text" name="search" placeholder="shipping cost" style="width: 120px; height: 30px;" /><br> <select name="selectSm" id="SelectLm" class="form-control-sm form-control" style="width: 120px;"> <option value="0">Please select</option> <option value="1">Option #1</option> <option value="2">Option #2</option> <option value="3">Option #3</option> <option value="4">Option #4</option> <option value="5">Option #5</option> </select> </td> <td> <button type="button" class="btn btn-primary btn-md" onclick="postitem()">Submit</button> </td> </tr> 

I would put a class on each input control you want to capture, then use that class to select only the inputs that you want. 我将在要捕获的每个输入控件上放置一个类,然后使用该类仅选择所需的输入。 For example I used .captureInput, and placed that class on each input and your select element. 例如,我使用了.captureInput,并将该类放置在每个输入和您的select元素上。

function postitem() {               
  $('#myform').find('.captureInput').each(function() {
    console.log($(this).val());
  });
}

This will get you all of the values. 这将为您提供所有价值。

With pure javascript, using document.getElementsByTagName : 使用纯JavaScript,使用document.getElementsByTagName

function findAll(){
var inputs = document.getElementsByTagName('input');
 for(var i = 0; i < inputs.length; i++){
   console.log(inputs[i].value);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM