使用find with data属性

时间:2013-09-24 07:37:28

标签: jquery

点击删除按钮后,我需要找到 data-extra-key 的值。我该怎么做?

<tr class="<%: classNames %>">
            <td data-extra-key="<%: item.ServiceKey %>">
                <%: item.ServiceName %>
                for
                <%: item.PetName %><sub><%: item.Description %></sub>
            </td>

            <td>
                <%if (item.IsAnExtra && !item.IsCancelled)
                  { %>
                <button class="btnRemoveExtraService actionButton secondaryButton short" type="button" itemid="<%:item.Id%>">
                    Remove</button>
                <%} %>
            </td>
        </tr>

我尝试过如下(我不知道如何使用查找 数据属性):

 $('.btnRemoveExtraService').die('click').live('click', function () {

                   var service= $(this).parents('tr').find('').val();

                     return false;

                });

4 个答案:

答案 0 :(得分:1)

var dataExtraKey = $(this)
                      .closest('tr')      //find the wrapping <tr>
                      .find('td:first')   //traverse down and get the first <td>
                      .data('extra-key'); //get the value

答案 1 :(得分:0)

试试这个

 var service= $(this).parents('tr').find('[data-extra-key]').data('extra-key');

答案 2 :(得分:0)

您可以像这样使用

            $('.btnRemoveExtraService').die('click').live('click', function () {

               var service= $(this).parent().parent().find('td:first-child').data('extra-key');

                 return false;

            });

希望这适合你。

答案 3 :(得分:0)

使用此代码:

$('.btnRemoveExtraService').click(function () {
   var service= $(this).closest('tr').find('td:first').data('extra-key');
   return false;
});