单击鼠标即可从HTML表中删除旧行并插入新行
remove old rows from HTML-table and insert new rows with one click
我有一个表格,当我点击搜索按钮时,它会显示结果。到目前为止,它是有效的,你可以在这里看到:
$('#search').click(function() {
data=[{"city":"Berlin","cStatus":"G"},{"city":"Hamburg","cStatus":"G"},{"city":"München","cStatus":"P"}];
if(data){
var len = data.length;
var txt = "<tbody>";
if(len > 0){
for(var i=0;i<len;i++){
if(data[i].city && data[i].cStatus){
txt += "<tr><td>"+data[i].city+"</td><td>"+data[i].cStatus+"</td></tr>";
}
}
if(txt != ""){
txt +="</tbody>";
$("#table").append(txt);
}
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<table id="table">
<thead>
<tr>
<th>City</th>
<th>Status</th>
</tr>
</thead>
</table>
<button id="search">suchen</button>
问题是,当我多次单击搜索按钮时,它会附加结果。它应该首先删除旧的行(或整个tbody(。我试过很多这样的东西:
var table = document.getElementById('table');
var row = document.getElementsByTagName('tbody')[0];
function deleteRow () {
row.parentNode.removeChild(row);
};
但它在我的例子中不起作用。即使我经常点击搜索按钮两次或两次以上,sobody也只能显示三行,这能成为一个有效的例子吗?
将append
替换为html
。
检查此处的代码
写入$("#table tbody"(.empty((;在附加之前
$('#search').click(function() {
data=[{"city":"Berlin","cStatus":"G"},{"city":"Hamburg","cStatus":"G"},{"city":"München","cStatus":"P"}];
if(data){
$("#table tbody").empty(); // Add this line before append. It will clear the previous data and appends new one.
var len = data.length;
var txt = "<tbody>";
if(len > 0){
for(var i=0;i<len;i++){
if(data[i].city && data[i].cStatus){
txt += "<tr><td>"+data[i].city+"</td><td>"+data[i].cStatus+"</td></tr>";
}
}
if(txt != ""){
txt +="</tbody>";
$("#table").append(txt);
}
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<table id="table">
<thead>
<tr>
<th>City</th>
<th>Status</th>
</tr>
</thead>
</table>
<button id="search">suchen</button>
$('#search').click(function() {
data=[{"city":"Berlin","cStatus":"G"},{"city":"Hamburg","cStatus":"G"},{"city":"München","cStatus":"P"}];
if(data){
var len = data.length;
var txt = "<tbody>";
if(len > 0){
for(var i=0;i<len;i++){
if(data[i].city && data[i].cStatus){
txt += "<tr><td>"+data[i].city+"</td><td>"+data[i].cStatus+"</td></tr>";
}
}
if(txt != ""){
txt +="</tbody>";
$("#table").html(txt);
}
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<table id="table">
<thead>
<tr>
<th>City</th>
<th>Status</th>
</tr>
</thead>
</table>
<button id="search">suchen</button>
$(".....").click(function (e) {
$("....").find('tr').remove();
$("....").find('thead').remove();
});
我想你也可以用这个。
相关文章:
- 如何从rails中的代码中删除新行( )
- JavaScript-如果以前不存在文本,如何从文本区域删除新行
- 使用删除文本创建新行
- 如何删除backgridjs行以显示新行
- 无法使用角度删除新创建的行.js
- 如何在使用Angular.js打开新行后添加删除按钮
- JQuery 数据表添加新行,如果该行已经存在,请将其删除并为该 ID 插入新行
- 如何在不影响 Jquery 数据表中其他行的情况下删除选定的现有数据并添加新行
- 从链接中删除新行
- AJAX:向表添加新行或使用 AJAX 删除
- 删除新行
- 删除文本区域中的新行并替换为值中的字符
- 删除字符串中javascript代码中的新行
- Regex删除空白和新行
- 在JQuery .html()调用中包含一个Twig模板并删除新行
- 单击鼠标即可从HTML表中删除旧行并插入新行
- 从顶部删除新的换行,并用剩余的文本替换
- 将文本区域的内容移到输入字段并移回来将删除新行.如何克服
- 如何修改我的正则表达式以不删除新行
- 剑道网格:取消编辑删除新行