如何将JQuery事件绑定到具有变量ID或类的选择器? [英] How to bind JQuery event to a selector with variable id or class?
问题描述
我有一个页面,该页面可以具有可变数量的表.每个表都有一个ID和与之关联的数字(例如,table1,table2,table3). 每个表中的一列包含一个我想将click事件绑定到的链接.我有一个变量,其中包含链接选择器的ID(例如,invlink1,invlink2,invlink3).如何将此变量选择器ID绑定到事件?
I have a page that can have a variable number of tables. Each table has an id with a number associated with it (for example, table1, table2, table3). A column in each table contains a link that I want to bind a click event to. I have a variable that holds the id for the link selector (for example, invlink1, invlink2, invlink3). How do I bind this variable selector id to the event?
我目前有:
"fnDrawCallback": function(){
console.log('table' + $(this).attr('id'));
var tid = $(this).attr('id');
console.log(tid);
var tabnum = tid.slice(7,8);
var invoicelink = "$(\'#invlink" +tabnum+ "\')";
console.log(invoicelink);
$('[id^="rnote"]').bind('click',addreasonnote);
invoicelink.bind('click',viewinvoicedetails);
$('[id^="reason"]').bind('change',selectreasoncode);
},
因此,invoicelink保存选择器的ID. 我还怀疑我应该为选择器使用类而不是ID,但这不会引起语法错误.
so, invoicelink holds the id of the selector. I also suspect I should be using a class instead of an id for the selector, but that's not causing the syntax error.
推荐答案
您可以在选择器中串联一个ID字符串:
You can concatenate an ID string in the selector:
$("#" + invoicelink)...
这篇关于如何将JQuery事件绑定到具有变量ID或类的选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!