简体   繁体   English

Javascript:解析html表

[英]Javascript: Parsing html table

I am working on a html page. 我正在处理html页面。 The page has a table with 3 columns and a button. 该页面有一个包含3列的表格和一个按钮。 One of the columns of the table is a check box (The number of rows are changed dynamically): 该表的一列是一个复选框(行数动态更改):

<table cellpadding="0" cellspacing="0" border="1" class="display" id="tag" width="100%">
    <tbody>
        <tr>
            <td align="center">
                <input type="checkbox" class="case" name="case" value="0"/>
            <td>fruit</td>
            <td>apple</td>
        </tr>
        <tr>
            <td align="center">
                <input type="checkbox" class="case" name="case" value="1"/>
            <td>fruit</td>
            <td>pear</td>
        </tr>
    </tbody>
</table>
<p><input type="button" value="Generate" onclick="generate()"></p>

When user click the "Generate" button, the generate() function will generate a special string base on the column of each row. 当用户单击“生成”按钮时,generate()函数将在每行的列上生成一个特殊的字符串。

My question is that how can I check if the "checkbox" row is checked or not? 我的问题是,如何检查“复选框”行是否被选中? I would like to filter those non-checked rows when I generate the string. 我想在生成字符串时过滤那些未检查的行。

Thanks. 谢谢。

Filter the rows based upon which have a checked checkbox: 根据已选中复选框的行进行过滤:

var rows = $(".case:checked").closest("tr");

This returns a jQuery object that contains all of your table rows housing checked checkboxes. 这将返回一个jQuery对象,其中包含您所有包含已选中复选框的表行。

Call getElementsByName will give you an array of references of your checkboxes. 调用getElementsByName将为您提供复选框的引用数组。 Loop the array in order to get the values. 循环数组以获取值。

var arr = document.getElementsByName("case");
for(i = 0; i < arr.length; i++){
   if(arr[i].checked){
      doSomething;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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