简体   繁体   English

我如何为 forwach 循环中的按钮创建事件侦听器

[英]how do i make a event listener for a button that is in a forwach loop

I have a for each loop where it loops through a table and the table has an id field which gets assigned to the button's id .我有一个for 每个循环,它循环遍历一个表,并且该表有一个id字段,该字段被分配给按钮的id I need to see which button gets clicked (I need to get the value of the id ).我需要查看点击了哪个按钮(我需要获取id的值)。 the id of the table is not 1,2,3,4,5 it is either a Q or A with a number;表的id不是1,2,3,4,5它是带有数字的QA eg one button's id might be: Q21 .例如,一个按钮的id可能是: Q21

This might get you started:这可能会让你开始:

 $('table tr td button').click(function(){ let tbl = $(this).closest('table').attr('id'); let row = $(this).closest('tr')[0].rowIndex; console.log(tbl+row); });
 .h2{font-size:1.5rem;} .h3{font-style:italic;}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <div class="h2">Table rowIndex is zero-based</div> <div class="h3">(First row number is zero)</div> <table id="Q"> <tr><td>Row 1</td><td><button>Row 1</button></td></tr> <tr><td>Row 2</td><td><button>Row 2</button></td></tr> <tr><td>Row 3</td><td><button>Row 3</button></td></tr> </table>

I wrote the earlier answer before I saw your latest comment reply.在看到您的最新评论回复之前,我写了较早的答案。 This new answer might be closer to what you need.这个新答案可能更接近您的需要。 I will leave the first answer so you can refer to it later if you wish.我将留下第一个答案,以便您以后可以参考。

 $('table tr td button').click(function(){ let buttId = this.id; console.log('You clicked: ' + buttId); });
 .h2{font-size:1.5rem;} .h3{font-style:italic;}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <div class="h2">This might be closer to your needs</div> <table id="Q"> <tr><td>Row 1</td><td><button id="Q1">Row 1</button></td></tr> <tr><td>Row 2</td><td><button id="Q2">Row 2</button></td></tr> <tr><td>Row 3</td><td><button id="Q3">Row 3</button></td></tr> </table>

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

相关问题 如何在JavaScript中的事件监听器中使for循环工作 - how to make a for loop work in an event listener in javascript 如何为单个表格单元格制作事件侦听器(onclick) - How do I make an event Listener (onclick) for a single Table Cell 如何让循环等待事件监听器? - How to make a loop wait for an event listener? 我想制作自己的事件循环侦听器,但它阻塞了调用堆栈如何像处理任何事件一样处理它 - i want to make my own event loop listener but it is blocking the call stack how to handel it like any event 如何添加事件监听器? - How do I add an event listener? 如何在ng-repeat循环内分配angularjs事件侦听器? - How do I assign an angularjs event listener inside an ng-repeat loop? 如何在 Kotlin/JavaScript 中制作按钮单击侦听器事件? - How to make button click listener event in Kotlin/JavaScript? 如何使用单击事件侦听器制作按钮切换显示? - How to make a button toggle display with click event listener? 如何将事件侦听器绑定到编辑工具栏中的“全部清除”按钮? - How do I bind an event listener to Clear All button in the edit toolbar? Javascript:如何更新我的事件监听器,仅在用户STOPS键入时进行更改? - Javascript: How do I update my Event Listener to only make a change when the user STOPS typing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM