0

I have an asp.net page - I am using jQuery datatables on it.

Part of the code is below:

                        <% foreach (SubmissionSearchResult result in SearchResults)
                           {%>
                        <tr data-name='<%=result.ID %>'>  

So each time a row is drew on screen I am adding a data-name of result ID to it. What I want then is on clicking the row retrieve this ID.

I had something like the below to start :

        $('#searchResultsTbl').on("click", "tbody tr", function () {
            var nTds = $('td', this);

            var id = $(nTds[0]).text().trim();

This worked when the id was in the first td in the row but now the columns can be dynammic and it may not be in the first column. So I wanted to add the id to each row and then get it from the row so I added it as a data-name but not sure on how to get the value back from it?

4

3 回答 3

1
            $('#searchResultsTbl').on("click", "tbody tr", function () {
                var id = $(this).data('name');
            });
于 2013-07-02T11:38:09.807 回答
0

在行的点击事件上添加javascript函数

喜欢

       <tr data-name='<%=result.ID %>' onclick='showid("<%=result.ID %>")'>  

现在在 javascript 中定义这个 showid 函数

        function showid(rowvalue)
         {
               alert(rowvalue);
          }
于 2013-07-02T11:41:18.183 回答
0

尝试这个

    $('#searchResultsTbl tr:gt(0)').click(function () {
        var this_row = $(this);
        var data_name = this_row.attr('data-name');//row attribute
        var first_td = $.trim(this_row.find('td:eq(0)').html());
        var second_td = $.trim(this_row.find('td:eq(1)').html());
        var third_td = $.trim(this_row.find('td:eq(2)').html());//and so on
    });
于 2013-07-02T11:40:58.197 回答