表行的jQuery条件选择器 [英] jQuery conditional selector for table rows

查看:95
本文介绍了表行的jQuery条件选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表格:

< td> item< / td>< td>订单代码< / td>< td>价格< / td>



我正在使用jQuery处理表格,它需要查找订单代码:

  $。each($('。productList tbody tr'),function(){
var orderCode = $(this).find 'td:eq(1)')。html()。trim();
// do stuff
});

如果没有产品,表格会显示一条消息:

< td colspan =3>没有产品要显示< / td>



上面的行导致jQuery函数被弹出。使用条件选择器忽略无产品行的最可靠方法是什么?是否存在 colspan =1 colspan没有设置的选择器,或者它需要什么?

解决方案

不要优化选择器,它不会很好地扩展,因为jQuery将不得不评估每个子元素。避免出现错误...

  $('。productList tbody tr')。each(function(){
)var orderCode = $(this).find('td:eq(1)');

if(orderCode.length> 0){//确保它存在
orderCode = orderCode.html()。trim();
// do stuff
}
});


I have a table with data in:

<td> item </td><td> order code </td><td> price </td>

I'm processing the table with jQuery which needs to find the order code:

$.each($('.productList tbody tr'), function() {
    var orderCode = $(this).find('td:eq(1)').html().trim();
    // do stuff
});

If there are no products, the table shows a message:

<td colspan="3"> There are no products to display </td>

The above row causes the jQuery function to bomb out. What's the most robust way to use a conditional selector to ignore the "no products" row? Is there a selector for colspan="1" or colspan is not set or whatever it would need to be?

解决方案

Don't refine your selector, it won't scale well because jQuery will have to evaluate every child element. Avoid the error instead...

$('.productList tbody tr').each(function() { 
   var orderCode = $(this).find('td:eq(1)');

   if(orderCode.length > 0) { // Make sure it exists
     orderCode = orderCode.html().trim(); 
     // do stuff 
   }
}); 

这篇关于表行的jQuery条件选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆