0

I have a table that has the following td:

<td>
      <div id="spcheck">
         <div id="check" 
              data-1='<%# Eval("sth1") %>' 
              data-2='<%# Eval("sth2") %>' 
              data-3='<%# Eval("sth3") %>'>
              <asp:CheckBox ID="chk1" runat="server"/>
          </div>
      </div>
</td>

This is the last one. It has custom attributes as you can see. I need to get the values data-1, data-2 and data-3 if have the checkbox cheked. I have the folowing code that gets each tr but I need to go inside the td and get the attribute values:

$('#ListView2_itemPlaceholderContainer tbody tr').each(function () {
                // I GET THE <TR>
                $(this).find('input:checkbox:checked').each(function () {
                    // I NEED THIS CODE
                    });
                });
            });

Thanks in advance.

4

3 回答 3

2

因为属性在 div 上,所以需要先找到 div 并获取 div 的属性。可以使用.attr()div 上的 来获取

 $(this).find('input:checkbox:checked').each(function () {
         var $closestDiv = $(this).closest('div');  // Find the div

         var $data1 = $closestDiv.attr('data-1');  // data-1
         var $data2 = $closestDiv.attr('data-2');  // data-2
         var $data3 = $closestDiv.attr('data-3');  // data-3
  });
于 2012-10-10T16:42:32.900 回答
1
$('#ListView2_itemPlaceholderContainer tbody tr').each(function () {
    $(this).find('input:checkbox:checked').each(function () {
           var dataElement = $(this).closest('[data-1]');

           var data1 = dataElement.data('1');
           var data2 = dataElement.data('2');
           var data3 = dataElement.data('3');
        });
    });
});
于 2012-10-10T16:43:21.030 回答
0
$(this).attr('data-1'); 

应该做的伎俩

参见attr 的jQuery 手册条目

于 2012-10-10T16:41:59.893 回答