简体   繁体   English

使用jquery从表中获取复选框上的html值

[英]get html value on checkbox click from a table using jquery

I am creating a dynamic table like below on page load after fetching data from database 从数据库中获取数据后,我正在页面加载时创建如下所示的动态表

<table border="1" id="tableView">
        <thead>
            <th></th><th>ID</th><th>Name</th><th>Description</th><th>Active</th><th>Release Date</th>
        </thead>

        <tbody>
        <%
        for(int i=0;i<result.size();i++)
        {
            %><tr><td><input class="tablechkbox" type="checkbox"/></td><%
            String[] row=pi.getResults(result,i,params);
            for(int j=0;j<row.length;j++)
            {
                %><td class="viewa"><%out.print(row[j]);%></td><%
            }
            %></tr><%
        } %>
        </tbody>
    </table>

This is what I am doing to fetch ID column. 这是我要提取ID列的操作。 Please help me fetch any specific column 请帮助我获取任何特定的列

$('#tableView tbody tr').live('click', function (event) {
        if ($('input.tablechkbox', this).is(':checked'))
        {
            alert(this.innerHTMl());
            /*$('.viewa', this).each(function() {
                alert(this.innerHTMl());
            });*/
        }
    });

Below is my jsp page screenshot 下面是我的jsp页面截图

在此处输入图片说明

alert($(this).find("td:eq(1)").html()); 警报($(本).find( “TD:EQ(1)”。)HTML()); should work 应该管用

Consider storing the row Id in your row using an HTML5 data- attribute (the prefix allows you to define arbitrary attributes that are "legal"): 考虑使用HTML5数据属性将行ID存储在行中(前缀允许您定义“合法”的任意属性):

String[] row = pi.getResults(result, i, params);
%>
<tr data-id="<%=row[0]%>">
    <td><input class="tablechkbox" type="checkbox" /></td>

That way, you can grab the Id directly from the clicked row: 这样,您可以直接从单击的行中获取ID:

$('#tableView tbody tr').live('click', function (event) {
    if ($('input.tablechkbox', this).is(':checked')) {
        var id = $(this).data("id")

You can use $(this).attr("data-id") in older versions of jQuery. 您可以在旧版jQuery中使用$(this).attr("data-id")

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

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