[英]Jquery get row value in a table
HTML:
<tbody id="JusticeCourtTable"></tbody>
我通過下面的jquery代碼將數據加載到頁面。我有如下輸入和按鈕。
$.getJSON('/CourtHouseManagement/LoadLawCourt/?cityId=' + id, function (result) {
$('#JusticeCourtTable').html('');
for (var i = 0; i < result.length; i++) {
var tablestring =
'<tr>' +
'<th>' + result[i].CourtID + '</th>' +
'<th><input type="text" name="JusticeCourt" id="JusticeCourt" value="' + result[i].Name + '" class="form-control" data-required="true"></th>' +
'<th><button type="button" class="btn btn-sm btn-primary" data-process="' + 0 + '" data-courtid="' + result[i].CourtID + '" data-name="' + result[i].Name + '">Update</button></th>' +
'<th><button type="button" class="btn btn-sm btn-primary" data-process="' + 1 + '" data-courtid="' + result[i].CourtID + '" data-name="' + result[i].Name + '">Delete</button></th>';
tablestring += '</tr>';
$("#JusticeCourtTable").append(tablestring);
}
});
我使用下面的jquery代碼發送上面的輸入值
$(document).on('click', '#JusticeCourtTable button', function () {
if ($(this).data('process') == 0) {
var cityId = id;
var lawCourtId = $(this).data('courtid');
var lawCourtName = $(this).data('name'); // PROBLEM HERE
window.location.href = '/CourtHouseManagement/UpdateLawCourt/?lawCourtId=' + lawCourtId + "&lawCourtName=" + lawCourtName + "&cityId=" + cityId;
}
});
我的問題 :
我怎樣才能獲得文本框的價值
<input type="text" name="JusticeCourt" id="JusticeCourt" value="' + result[i].Name + '" class="form-control" data-required="true">
var lawCourtName = $(this).data('name');
總是得到默認值。如果我輸入一個新值,我無法通過使用此代碼獲得新值。如何獲得數據的新值('名稱')?
任何幫助將不勝感激。
如果你想獲得在文本框中輸入的值,那么你必須這樣做:
$(this).closest('tr').find('input:text').val()
使用closest()
獲取parent tr,然后使用find()
獲取其中的輸入字段並獲取其值
要獲取textbox
name
屬性,請嘗試:
....
......
var lawCourtName = $('#JusticeCourt').attr('name');//get textbox name
var lawCourtVal = $('#JusticeCourt').val();//get textbox value
.....
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.