我有一个动态表,我需要从脚本中的任何位置获取日期......
表格
$('#update_panel').html('Loading Date....');
$.ajax({
url: '/Home/GetCountries',
type: 'GET',
datatype: 'Json',
success: function (data) {
if (data.length > 0) {
var $data = $('<table></table>').addClass('table table-responsive table-striped');
var header = "<thead><tr><th>Country ID</th><th>Country</th></tr></thead>";
$data.append(header);
$.each(data, function (i, row) {
var $row = $('<tr/>');
$row.append($('<td/>').html(row.CountryId));
$row.append($('<td/>').html(row.CountryName));
$row.append($('<td/>').html("<a href='/home/Save/" + row.CountryId + "' class='popup'>Edit</a> | <a href='/home/Delete/" + row.CountryId + "' class='popup'>Delete</a>"));
$data.append($row);
});
$('#update_panel').html($data);
}
我试过了,没用
$("#mytable tr").click(function () {
var countryId = $(this).find('td').eq(1).text().trim();
alert(countryId);
});
如何到达 Dom 中的表?
提前致谢
最佳答案
$.ajax({
url: '/Home/GetCountries',
type: 'GET',
datatype: 'Json',
success: function (data)
{
if (data.length > 0)
{
// HERE I HAVE APPLIED 'id' ATTRIBUTE TO TABLE
var $data = $('<table id="mytable"></table>').addClass('table table-responsive table-striped');
var header = "<thead><tr><th>Country ID</th><th>Country</th></tr></thead>";
$data.append(header);
$.each(data, function (i, row)
{
var $row = $('<tr/>');
$row.append($('<td/>').html(row.CountryId));
$row.append($('<td/>').html(row.CountryName));
$row.append($('<td/>').html("<a href='/home/Save/" + row.CountryId + "' class='popup'>Edit</a> | <a href='/home/Delete/" + row.CountryId + "' class='popup'>Delete</a>"));
$data.append($row);
});
$('#update_panel').html($data);
}
}
});
//USE 'on' EVENT WHEN YOU WANT TO TRIGGER EVENT ON DYNAMIC DOM
$("#update_panel").on("click", "#mytable tr", function ()
{
// IT WILL SELECT ALL 'td' CHILDREN OF CLICKED 'tr' ELEMENT.
// THEN WE USED .first() SO IT WILL SELECT ONLY FIRST 'td' ELEMENT
var countryId = $.trim($(this).children("td").first().text());
alert(countryId);
});
关于javascript - 如何从动态表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32900286/