简体   繁体   English

如何从td Jquery中的Input字段读取值

[英]How to Read the Values From Input field inside td Jquery

I have a table and with in tds i had a text box 我有一张桌子,在tds中有一个文本框

        <table class="table ratemanagement customtabl-bordered " id="rate_table">
       <tbody>
          <tr>
             <th><input type="checkbox" onclick="select_all()" class="check_all"></th>
             <th>From Days*</th>
             <th>To Days*</th>
             <th>Rent*</th>
          </tr>
          <tr>
             <td>
                <input class="case" type="checkbox">
             </td>
             <td class="v">
                <input id="rate_fromdays" class="form-control" name="fromdays" type="text">
             </td>
             <td>
                <input id="rate_todays" class="form-control" name="todays" type="text">
             </td>
             <td>
                <input id="rate_rent" class="form-control" name="rent" type="text">
             </td>
          </tr>
<tr>
             <td>
                <input class="case" type="checkbox">
             </td>
             <td class="v">
                <input id="rate_fromdays" class="form-control" name="fromdays" type="text">
             </td>
             <td>
                <input id="rate_todays" class="form-control" name="todays" type="text">
             </td>
             <td>
                <input id="rate_rent" class="form-control" name="rent" type="text">
             </td>
          </tr>
       </tbody>
    </table>

i want to read the values from text box i tried 我想从我尝试过的文本框中读取值

var values = {};
    $('.v input').each(function () {
        values[$(this).attr('name')] = $(this).val();
    });

and

$('input[name="fromdays"],[name="todays"],[name="rent"]').each(function () {
      var fromdays = $(this).val();
      alert(fromdays);
 });

I want to store the values in independent variables how do i do that ex all fromdays to firstvariable, all todays to second variable how do i do that Thanks 我想将值存储在自变量中,从前一天到第一个变量我该怎么办,从今天到第二个变量,我该怎么办?

var values = [];
    $('.v input').each(function () {
        values.push($(this).attr('name') = $(this).val());
    });

You can store the values in three different arrays by checking the name attribute of the input fields 您可以通过检查input字段的name属性将值存储在三个不同的arrays

var fromdays=new Array();
var todays=new Array();
var rent=new Array();
$('#rate_table input[type="text"]').each(function () {

    if($(this).attr('name')=="fromdays")
        fromdays.push($(this).val())

    if($(this).attr('name')=="todays")
        todays.push($(this).val())


    if($(this).attr('name')=="rent")
        rent.push($(this).val())

 });

JsFiddle 的jsfiddle

On button click, iterate over the inputs and push their values in to the respective array: 单击按钮时,遍历输入并将其值推入相应的数组:

 $(document).on('click', '#getVar', function() { var fromVar = []; var toVar = []; $('input[name=fromdays]').each(function() { fromVar.push($(this).val()); }); $('input[name=todays]').each(function() { toVar.push($(this).val()); }); alert('from: ' + fromVar + ' - to: ' + toVar); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table class="table ratemanagement customtabl-bordered " id="rate_table"> <tbody> <tr> <th><input type="checkbox" onclick="select_all()" class="check_all"></th> <th>From Days*</th> <th>To Days*</th> <th>Rent*</th> </tr> <tr> <td> <input class="case" type="checkbox"> </td> <td class="v"> <input id="rate_fromdays" class="form-control" name="fromdays" type="text"> </td> <td> <input id="rate_todays" class="form-control" name="todays" type="text"> </td> <td> <input id="rate_rent" class="form-control" name="rent" type="text"> </td> </tr> <tr> <td> <input class="case" type="checkbox"> </td> <td class="v"> <input id="rate_fromdays" class="form-control" name="fromdays" type="text"> </td> <td> <input id="rate_todays" class="form-control" name="todays" type="text"> </td> <td> <input id="rate_rent" class="form-control" name="rent" type="text"> </td> </tr> </tbody> </table> <button id="getVar">get variables</button> 

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

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