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

时间:2015-04-17 06:39:49

标签: javascript jquery jquery-plugins

我有一张桌子,并且在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>

我想从我试过的文本框中读取值

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

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

我想将这些值存储在自变量中,我是如何做到这一点从前一天到第一个变量,所有今天到第二个变量 我怎么做 感谢

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

您可以通过查看arrays字段的name属性

,将值存储在三个不同的input
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

答案 2 :(得分:0)

单击按钮,迭代输入并将其值推入相应的数组:

&#13;
&#13;
$(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);
});
&#13;
<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>
&#13;
&#13;
&#13;