gpt4 book ai didi

javascript - 无法从 HTML 获取表值

转载 作者:行者123 更新时间:2023-12-02 15:23:37 25 4
gpt4 key购买 nike

我想在这里获取此信息:

Here is what I want

因为我需要它从数据库(主键)获取数据。但获得这个值是很棘手的。文档准备就绪后,我运行以下 JavaScript 来创建表:

function GetActivityAbstracts() {
$.getJSON("http://localhost:52535/ExampleService.svc/GetTestTableData", function (testData) {
var object = $.parseJSON(testData);
var activityTable = $('#activityTable');

// create HTML only by using $.map, it's better for perfs to generate all HTML before adding it into the dom
var html = $.map(object, function (item, index) {
//index here is used to generate unique ids
var activityId = item['ActivityId'];
var activityName = item['ActivityName'];
var activityResponsible = item['Responsible'];
var activityEstimatedSavings = parseFloat(item['EstimatedSavings']).toFixed(2);
var activityEstimatedStart = item['EstimatedStart'];
var activityEstimatedEnd = item['EstimatedEnd'];
var activityStatus = item['Status'];
// TODO: Make more user-friendly Status Descriptions instead of C# enum values.
// move id attr on on <tr>
return '<tr id="activityId_' + index + '">' +
'<td data-id="' + activityId +
'" style = "vertical-align: middle; align: center;">'
+ activityId + '</td>' +
'<td style = "vertical-align: middle;">' +
'<div class="status-circle" data-toggle="tooltip" data-placement="right"' +
'title=" ' + activityStatus + '" style="background-color:' +
GetColumnColor(activityStatus) + ';"></div></td>' +
'<td id = "activityName_' + index + '" style = "vertical-align: middle;">'
+ activityName + '</td>' +
//Add index for ids here
'<td style = "vertical-align: middle;">'
+ activityResponsible + '</td>' +
'<td style = "vertical-align: middle;">'
+ activityEstimatedSavings + '</td>' +
'<td style = "vertical-align: middle;">'
+ activityEstimatedStart + '</td>' +
'<td style = "vertical-align: middle;">'
+ activityEstimatedEnd + '</td>' +
'</tr>';

});
// Add HTML into activityTable
activityTable.html(html);
// Add event by using delegate event.
debugger
AddActivityAbstractOnClickEvent();
$('#current-data-table').append(activityTable);

/* This call is necessary because the table is added dynamically */
$('[data-toggle="tooltip"]').tooltip();
});
}

然后我运行这个:

function AddActivityAbstractOnClickEvent() {
$('#activityTable').on('click', 'tr', function () {
var id = $(this).find('td').find('data-id').children();
GetActivityFullDescription(id);
});
}

function GetActivityFullDescription(id) {
alert(id);
}

作为一个简单的测试,以确保我获得了正确的 ID。但这样做并不能让我获得 ID,而且我还觉得我做得太复杂了._.

运行上面的代码,我在调试器中看到下面的这个值。但我无法访问它,这可能是因为我一开始就做错了。

I want this from the debugger

我试图绕过我使用的这种选择方法,是将“data-id”添加到 <td>元素,然后访问它,但这并没有真正起作用......有什么想法吗?

最佳答案

如果需要获取第一个td,可以使用

 $('#activityTable').on('click', 'tr', function () {
var id = $(this).find('td:first').data('id');
GetActivityFullDescription(id);
});

关于javascript - 无法从 HTML 获取表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33870739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com