简体   繁体   中英

getting specific table rows based on the value within an input being greater than zero

I need to get the number of rows in a table where an input within the row has a value greater than zero. so for example i have

<table>
 <tr><td><input type="text" value="23.4"/></td></tr>
 <tr><td><input type="text" value="39.5"/></td></tr>
 <tr><td><input type="text" value="14.4"/></td></tr>
 <tr><td><input type="text" value="0"/></td></tr>
 <tr><td><input type="text" value="89"/></td></tr>
</table>

so the query must return a total row count of 4 using the above example.

I have managed to get the following to work as the quickest and easiest solution to this problem

var counter = 0;
$('table tr').each(function(){
   if(parseFloat($(this).find('input').val()) > 0){counter++;}
});

the value increments for each value found greater than zero. thanks for the help guys

Get the rows, then filter on inputs that you filter on wether they have a value greater than zero.

var numb = $('table tr').filter(function() {
    return $(this).find('input').filter(function() {
        return this.value > 0;
    }).length > 0;
}).length;

This would allow mulitple inputs in each row, and if any input has a value greater than zero, the row is counted

FIDDLE

well you need jQuery, then retrieve all '.tr input', then check the value

var res = $('tr input').filter(function(index){
   // need multiplicate by 1 to cast as number because input values are always strings
   return (this.value*1) > 0;
});
console.log('res.length : ', res.length);

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