使用javascript检查我的html表是否为空
如何检查我的html表是否为空。请记住,下表必须视为空表,因为除标题外没有行使用javascript检查我的html表是否为空,javascript,html,css,Javascript,Html,Css,如何检查我的html表是否为空。请记住,下表必须视为空表,因为除标题外没有行 <table style="float: left;text-align: center;" id="Table1""> <th style="width: 11%;">Item Id</th> <th style="width: 44%;">Item Name</th> <th style="width: 11%;">
<table style="float: left;text-align: center;" id="Table1"">
<th style="width: 11%;">Item Id</th>
<th style="width: 44%;">Item Name</th>
<th style="width: 11%;">Quantity</th>
<th style="width: 18%;">Trade Price</th>
<th style="width: 16%;">Price</th>
<th style="width: 7%;" id="non-printable">Delete</th>
</table>
我在计数器中对每一行进行递减删除并检查它。如果为零,则另一个计数器将设置为零
function deleteRow(id)
{
var priceID="price"+id;
//alert(priceID);
var value=document.getElementById(priceID).innerHTML;
//alert(value);
var totalWithoutDiscount1=document.getElementById("Total_without_discount").value;
var newTotal=(parseFloat(totalWithoutDiscount1)-parseFloat(value)).toFixed(2);
//alert(newTotal);
document.getElementById("Total_without_discount").value=newTotal;
document.getElementById("Table1").deleteRow(id-counter);
document.getElementById("Table2").deleteRow(id-counter);
count--;
counter++;
if(count==0)
{
counter=0;
alert(counter);
}
}
您可以使用表的行和长度属性来确定有多少行
var rows = $('#Table1').rows.length;
可以统计表体中的行数。相应地摆好你的桌子
console.log(document.querySelectorAll('Table1 tbody tr').length);//0
document.querySelector('#Table1 tbody').appendChild(document.createElement('TR'));
console.log(document.queryselectoral('#Table1 tbody tr').length);//1
项目Id
项目名称
量
交易价格
价格
删除
如果要检查表是否为空,则需要检查行数是否为0
if ($('#Table1 tr').length == 0) {
//...do something here
}
如果一个表只包含一行,就知道它是空的(因为一行是在表标题周围动态添加的)。因此,您可以尝试选择表中的所有行,并检查返回的集合的长度。如果长度小于或等于1,则表示该表为空。否则,如果它较大,则您知道它在表中有行
见下例:
const isEmpty=document.queryselectoral(#Table1 tr”).length您能添加您迄今为止在javascript中尝试过的内容吗?您解决问题的方法是错误的。您正在更新该表,以便知道它何时何地为空。不要更新DOM,然后检查DOM以了解您之前所做的事情,并采取相应的行动。在一个地方做所有依赖于DOM更新的事情。@Prashant我已经添加了一个对我有用的答案为什么这样做-它不是普通的JavaScript。jQuery不是JavaScript问题的解决方案。@treyBake在添加注释之前,最好先彻底阅读问题。OP问:如何使用javascript或jquery检查表是否为空?
。