[英]How to Read the Values From Input field inside td Jquery
我有一張桌子,在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);
});
我想將值存儲在自變量中,從前一天到第一個變量我該怎么辦,從今天到第二個變量,我該怎么辦?
var values = [];
$('.v input').each(function () {
values.push($(this).attr('name') = $(this).val());
});
您可以通過檢查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())
});
單擊按鈕時,遍歷輸入並將其值推入相應的數組:
$(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.