gpt4 book ai didi

javascript - 在 JQuery 中区分此代码段中的 2 个复选框

转载 作者:太空宇宙 更新时间:2023-11-04 04:01:29 26 4
gpt4 key购买 nike

我有一个带有复选框的表格。我配置了一个函数来在单击复选框时执行某些功能,如下所示:

$("#table").on('click', ":checkbox", function() { …. });

现在我需要在表中添加另一个复选框。如何将复选框的名称添加到此代码片段,以便我可以区分这 2 个?
请注意,这是每行一个复选框,每行 2 个复选框。所以我不能使用 id。仅 nameclass

最佳答案

通过使用 attribute-equals-selector :

$("#table").on('click', ":checkbox[name=meh]", function() { ... });

或者使用类名:

$("#table").on('click', ":checkbox.meh", function() { ... });

PS:我建议将 :checkbox 替换为 input[type=checkbox] 以提高性能:

Because :checkbox is a jQuery extension and not part of the CSS specification, queries using :checkbox cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use [type="checkbox"] instead.

关于javascript - 在 JQuery 中区分此代码段中的 2 个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22013790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com