0

I found javascript example on this site that hides rows based on reading a value directly within a cell within the row, but I need it to read the value within a text input field.

Here's the javascript I'm using:

function hide() {
var tbl = document.getElementById('Table'),
    rows = tbl.tBodies[0].children, l = rows.length, i,
    filter = function(cells) { 
        var values = [
            parseInt(cells[0].firstChild.nodeValue.replace(/,/g,''),10),
            parseFloat(cells[1].firstChild.nodeValue.replace(/,/g,''))
        ];
        if( values[1] < 1) return false;
        return true;
    };
for( i=0; i<l; i++) {
    if( !filter(rows[i].cells)) rows[i].style.display = "none";
}
}

Here's the HTML:

 <tr>
<tdRow 1</td>
<td>1<input id="Quantity1" name="Quantity1" class="numericValue" type="text" value="1" /></td>
</tr>
<tr>
<td>Row 2</td>
<td>2<input id="Quantity2" name="Quantity2" class="numericValue" type="text" value="2" /></td>
</tr>
<tr>
<td>Row 3</td>
<td>3<input id="Quantity3" name="Quantity3" class="numericValue" type="text" value="0" /></td>
</tr>
<tr>
<td>Row 4</td>
<td>4<input id="Quantity4" name="Quantity4" class="numericValue" type="text" value="0" /></td>
</tr>
<tr>
<td>Row 5</td>
<td>0<input id="Quantity5" name="Quantity5" class="numericValue" type="text" value="0" /></td>
</tr>

<input id="btnSubmit" onclick="hide();" type="submit" value="Hide" /></p>

As is, the js will read the "0" in Row 5 and hide it, but I want Rows 3,4, and 5 with the input values of "0" to be hidden. How do I get at the input value? this is for a table in which the input values will all start at "0" (for a quantity), then one or more of the values will be changed.

Thanks.

4

1 回答 1

0

Just need to change a single line:

parseFloat(cells[1].firstChild.nodeValue.replace(/,/g,''))

becomes

parseFloat(cells[1].firstElementChild.value.replace(/,/g,''))

Fiddle

于 2013-05-14T18:09:04.630 回答