0

尝试在单击帐户或账单链接时从下面的 html / 脚本中的表 id="1" 中获取表 ID 我想获取表 ID,以便我可以将该表 ID 传递给另一个函数以获取 AJAX 数据。表 ID 是装箱测功机。这是测试数据

<html>
<head>
<script src="jquery-1.8.2.min.js"></script>
<script type = "text/javascript">
$(document).ready(function() {                  
                $('.toggler').click(function(event){
                    event.preventDefault();
                    $(this).parent().find('.content').slideToggle();
                    var a = $(this).next("div").find(".content").children().attr("id");
                    alert(a);

                }); 
</script>
</head>
<body>
<table border="1">
    <tr>
        <td>
            <table id="MainTable">
                <tr>
                    <td>
                        <div class="toggler-wrap">
                            <a href="#" class="toggler">Account </a>
                            <a href="#" class="toggler">Bill</a>
                            <div class="content">   
                                <table id="1">
                                    <tr><td>Content will come here</td></tr>                                    
                                </table>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td>
            <table id="MainTable">
                <tr>
                    <td>
                        <div class="toggler-wrap">
                            <a href="#" class="toggler">Account</a>
                            <a href="#" class="toggler">Bill</a>
                            <div class="content">   
                                <table id="2">
                                    <tr><td>Content will come here</td></tr>                                    
                                </table>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    </table>
</body>
</html>
4

3 回答 3

1

你可以像这样得到它

$('.content').find('table').attr('id');

更新代码:

 $('.toggler').click(function(event){
      event.preventDefault();
      $(this).parent().find('.content').slideToggle();
      var a = $(this).parent().find(".content").find('table').attr('id');
      alert(a);
 }); 

更新了 Jsfiddle

于 2012-10-17T11:20:09.670 回答
1
$('.toggler').click(function(event){
   event.preventDefault();
   $(this).parent().find('.content').slideToggle();
   var a = $(this).parent().find('table').attr("id");
   alert(a);
});
于 2012-10-17T11:27:30.283 回答
0

我会为每个包含您要搜索的 Id 的链接添加一个属性,然后使用它来查找您要查找的特定表。

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type = "text/javascript">
$(document).ready(function() {                  
                $('.toggler').click(function(event){
                    event.preventDefault();
                    var targetId = $(this).attr('data-id');
                    //Call ajax with the target Id

                    var content = $('table[id="' + targetId + '"] td').text();
                    alert(content);

                    $(this).parent().find('.content').slideToggle();
                }); 
            });
</script>
</head>
<body>
<table border="1">
    <tr>
        <td>
            <table id="MainTable">
                <tr>
                    <td>
                        <div class="toggler-wrap">
                            <a href="#" class="toggler" data-id="1">Account </a>
                            <a href="#" class="toggler" data-id="1">Bill</a>
                            <div class="content">   
                                <table id="1">
                                    <tr><td>Content for id 1</td></tr>                                    
                                </table>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td>
            <table id="MainTable">
                <tr>
                    <td>
                        <div class="toggler-wrap">
                            <a href="#" class="toggler" data-id="2">Account</a>
                            <a href="#" class="toggler" data-id="2">Bill</a>
                            <div class="content">   
                                <table id="2">
                                    <tr><td>Content for id 2</td></tr>                                    
                                </table>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    </table>
</body>
</html>
于 2012-10-17T11:34:27.590 回答