gpt4 book ai didi

jQuery 替换表主体内容

转载 作者:行者123 更新时间:2023-12-03 21:59:22 24 4
gpt4 key购买 nike

我的 jQuery 脚本似乎有问题。我想用新的“tbody”内容替换当前的“tbody”内容。目前只是继续向当前数据添加,而不是删除旧数据并插入新数据。有什么想法吗?

这是我的代码:

function getData(action,searchVal) {    
$.get('ajax.php',{action:action,value:searchVal}, function(data){
var json = jQuery.parseJSON(data);
$(function () {
var content = '';
content += '<tbody>';
for (var i = 0; i < json.length; i++) {
content += '<tr id="' + json[i].ID + '">';
content += '<td><input id="check_' + json[i].ID + '" name="check_' + json[i].ID + '" type="checkbox" value="' + json[i].ID + '" autocomplete=OFF /></td>';
content += '<td>' + json[i].ID + '</td>';
content += '<td>' + json[i].Name + '</td>';
content += '<td>' + json[i].CountryCode + '</td>';
content += '<td>' + json[i].District + '</td>';
content += '<td>' + json[i].Population + '</td>';
content += '<td><a href="#" class="edit">Edit</a> <a href="#" class="delete">Delete</a></td>';
content += '</tr>';
}
content += '</tbody>';
$('table tbody').replaceWith(content);
});
});
};

最佳答案

function getData(action,searchVal) {    
$.get('ajax.php',{action:action,value:searchVal}, function(data){
var json = jQuery.parseJSON(data);
$(function () {
var content = '';
//content += '<tbody>'; -- **superfluous**
for (var i = 0; i < json.length; i++) {
content += '<tr id="' + json[i].ID + '">';
content += '<td><input id="check_' + json[i].ID + '" name="check_' + json[i].ID + '" type="checkbox" value="' + json[i].ID + '" autocomplete=OFF /></td>';
content += '<td>' + json[i].ID + '</td>';
content += '<td>' + json[i].Name + '</td>';
content += '<td>' + json[i].CountryCode + '</td>';
content += '<td>' + json[i].District + '</td>';
content += '<td>' + json[i].Population + '</td>';
content += '<td><a href="#" class="edit">Edit</a> <a href="#" class="delete">Delete</a></td>';
content += '</tr>';
}
// content += '</tbody>';-- **superfluous**
//$('table tbody').replaceWith(content); **incorrect..**
$('#myTable tbody').html(content); // **better. give the table a ID, and replace**
});
});
};

如果您不学会正确定位替换,您最终可能会得到多个表并替换两个表的内容。另外,由于您要替换 tbody 内容,因此您无法在其内部添加另一级别的 tbody...

关于jQuery 替换表主体内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4453145/

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