jQuery search each table row for a particular td
Consider there's a table with rows to which a td
with class x
is added by jQuery on the fly (during DOM manipulation). Later when the user submit's this table (table being part of a form), i want to search all the rows for existence of this particular td
. If this td
is found then the function should return false, else true. However this piece of code does not work, any suggestions开发者_如何学JAVA?
function validate(){
$('form#newuser table tr').each(function(){
if($(this).find('td.x')){
return false;
}
});
return true;
}
function validate() {
return ($('form#newuser table tr td.x').length == 0);
};
$(<selector>).length
returns the number of DOM elements matched by the <selector>
.
you can forget the each loop, too and really shorten this up since your selector will find ALL tr's (it's a set)...
function validate(){
if($('form#newuser table tr.x').length){
return false;
}
});
return true;
}
i havent tested this, but it should work.
Try this code...
$('#YourFormName TABLE TBODY TR').each(function()
{
if $(this).children('td').hasClass('x')
{
return false;
}
});
精彩评论