根据文本输入中的值隐藏表格行

时间:2013-05-13 20:44:40

标签: javascript

我发现这个网站上的javascript示例基于直接读取行内单元格内的值来隐藏行,但我需要它来读取文本输入字段中的值。

这是我正在使用的javascript:

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

这是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>

按原样,js将读取第5行中的“0”并隐藏它,但我希望隐藏输入值为“0”的行3,4和5。我如何获得输入值?这是针对一个表,其中输入值将全部从“0”开始(对于数量),然后将更改一个或多个值。

感谢。

1 个答案:

答案 0 :(得分:0)

只需更改一行:

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

变为

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

Fiddle

相关问题