如何获得索引表行,当我单击工具提示菜单模板中的项

How to get the index - table row -, when I click on li item in tooltip menu template

本文关键字:菜单 工具提示 单击 索引 何获得      更新时间:2023-09-26

下面是我使用的代码。在我的表格中,将鼠标悬停在销售栏上,我有一个菜单模板工具提示,其中包含3个列表项。当单击其中一个li项时,我想知道sales列所在的表行/索引。下列代码:

<td>
    <a href='#' class = 'sales_tooltip' data-templatename='Conifer' 
        data-title='<div class = "tooltip-menu">
                    <ul><li class="sales_rep"><a href="#">Sales Rep 1</a></li>
                    <li class="sales_rep"><a href="#">Sales Rep 2</a></li>
                    <li class="last-child sales_rep"><a href="#">Sales Rep 3</a></li>
                    </ul>
                    </div>'> 
        <i class = 'fa fa-male ' aria-hidden='true'></i>    
    </a>
</td>

我想要行的tr索引,我单击了行的li项工具提示。javascript代码

$('body').on('click', '.tooltip-menu li.sales_rep', function() {
    if(confirm("Have you chosen your sales rep?")){
        var sales_rep = $(this).find("a").html();
        //var $index = $(this).closest('a').closest('tr').index();
        var $index = $('#'+ $(this).data('for')).closest('tr').index();
    }

我试过了,但这不起作用,因为它返回-1。请帮助

如果您正在使用jQuery UI中的Tooltip,根据我的经验,实际的工具提示div是作为工具提示触发器元素的兄弟而不是子元素生成的。这意味着您的工具提示菜单是作为锚(a)的兄弟生成的,这样当您调用.closest('a')时,它将不会返回任何内容,因为工具提示没有匹配该选择器的父项。

试试var index = $(this).closest('tr').index();