简体   繁体   English

如何在Jquery中的click事件上传递元素的ID

[英]How To Pass id of an element on click event in Jquery

I have a table with number of rows and I am using with click to show hidden rows. 我有一个具有行数的表,并且正在使用click来显示隐藏的行。 In each hidden rows I have a div with name content. 在每个隐藏的行中,我都有一个包含名称内容的div。 That in this div I am using ajax to load second page. 在这个div中,我正在使用ajax加载第二页。 Actually when I click to Show button the second page will be appear in the div with classname content. 实际上,当我单击“显示”按钮时,第二页将显示在具有类名内容的div中。 My question :- is there a way to passing id of each row (the id of each row is in input hidden with name id) to the function on click event then use of that passed id in href to send to second page ? 我的问题:-是否有一种方法可以将每行的ID(每行的ID在名称隐藏的输入中)传递给click事件上的函数,然后在href中使用该传递的ID发送到第二页? Here is my Snippet :- 这是我的片段:

 $(function() { $(".show").on("click", function(e) { var id = $(this).find(':hidden').val(); e.preventDefault(); $(this).closest("tr").next().find(".content").slideToggle(); $(".content").load("/secondpage.htm?id="+id+""); alert(id) }); }); 
 .subRow { padding:0; background-color: #CFCFCF; } .content { background-color: #CFCFCF; display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <table style="width:50%" border="1"> <tr> <td>title</td> <td>title</td> <td>title</td> <td><a href="#" class="show"><input type="hidden" val="123" name="id"/>Show </a></td> </tr> <tr id="tr_2_1_0" class="subRow"> <td colspan="6"><div class="contentdiv"></div></td> </tr> <tr> <td>title</td> <td>title</td> <td>title</td> <td><a href="#" class="show"><input type="hidden" val="123" name="id"/>Show </a></td> </tr> <tr id="tr_2_1_0" class="subRow"> <td colspan="6"><div class="contentdiv"></div></td> </tr> </table> 

Thanks 谢谢

You can use .find() / .children() to target the hidden element, then .val() can be used. 可以使用.find() / .children()为目标的hidden元件,然后.val()都可以使用。

$(".show").on("click", function(e) {
    e.preventDefault();
    var id = $(this).find(':hidden').val();
    $(".content").load("/secondpage.htm?id=" + id);
    console.log(id )
});

 $(function() { $(".show").on("click", function(e) { e.preventDefault(); console.log($(this).find(':hidden').val()) $(this).parent().toggleClass('rotati') $(this).closest("tr").next().find(".content").slideToggle(); $(".content").load("/secondpage.htm?id=@id"); }); }); 
 .subRow { padding:0; background-color: #CFCFCF; } .content { background-color: #CFCFCF; display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <table style="width:50%" border="1"> <caption>Ajax Test Table</caption> <thead> <tr align="center" class="parentRow"> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> </tr> </thead> <tbody> <tr class="parentRow"> <td><a href="#" class="show">SHOW <input type="hidden" value="123" name="id"/></a> </td> <td>test cell</td> <td>test cell</td> </tr> <tr class="subRow"> <td colspan="5"> <div class="content"><p></p></div> </td> </tr> <tr class="parentRow"> <td><a href="#" class="show">SHOW <input type="hidden" value="456" name="id"/></a> </td> <td>test cell</td> <td>test cell</td> </tr> <tr class="subRow"> <td colspan="5"> <div class="content"><p></p></div> </td> </tr> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM