简体   繁体   中英

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?

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;
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM