简体   繁体   English

将jquery .on事件绑定到具有动态ID的动态按钮

[英]binding jquery .on event to dynamic button with dynamic id

I have a situation where i need to bind an onclick event to a button which is being dynamically injected into the main html document by an external javascript. 我有一种情况,我需要将onclick事件绑定到由外部javascript动态注入到主html文档中的按钮。

The problem is that that the class id of the button is suffixed with some dynamic numbers which change every time the page is reloaded. 问题在于按钮的类ID带有一些动态数字后缀,每次重新加载页面时,这些数字都会改变。

Please see example: 请参见示例:

<button class="clickme12345" type=submit value=clickme>send</button> 

Wo the situation is that on every page reload the numbers for the class id for the button will change so next time it will be clickme67890. 情况是,在每个页面上重新加载按钮的类ID的数字都会更改,因此下一次将是clickme67890。

Would there be any way to get the jQuery binding to work for this situation? 是否有任何方法可以使jQuery绑定在这种情况下工作?

Any reply would be greatly appreciated. 任何答复将不胜感激。

If this is really how you want to use it, you could use an attribute selector 如果确实要使用它,则可以使用属性选择器

Something like [attr*=value] where the attr contains the string value . 类似[attr*=value] ,其中attr包含字符串value

For example (in jQuery): 例如(在jQuery中):

var $btnElement = $('[class*="clickme"]');

The above will select any elements that have a class attribute containing clickme 上面的内容将选择具有class属性包含clickme任何元素

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

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