繁体   English   中英

Jquery在表中获取行值

[英]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.

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