通过 JQuery,如何指定 on 函数作为许多单选按钮的 onClick 处理程序?
我有一个名为 handleOnClickRadio(i, j);
的函数和许多名为 id="something-radio[i][j]"
的单选按钮。所有这些单选按钮都位于名为“bigtable”的表中。
如何将函数 handleOnClickRadio()
附加到所有这些单选按钮?并使用 handleOnClickRadio(i,j)
正确调用它。
谢谢。
I have a function called handleOnClickRadio(i, j);
and lots of radio buttons named as id="something-radio[i][j]"
. All these radio buttons are in a table called "bigtable".
How could I attach the function handleOnClickRadio()
to all these radio buttons? And call it correct with handleOnClickRadio(i,j)
.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您有很多无线电,我建议使用委托:这样,只会附加一个事件侦听器,
请参阅 http:// api.jquery.com/delegate/
I suggest using delegate if you have lot of radios: that way, only one Event listener will be attached
see http://api.jquery.com/delegate/
理想情况下,您应该将 onclick 分配给大表而不是每个单选按钮。 JavaScript 中的事件会冒泡,因此表(所有这些单选按钮的最终父级)将接收该事件。
因此,在 jQuery 中,您将拥有这样的代码:
您的 handleOnClickRadio 函数的签名将是
evt.target 将标识单击/选中的实际单选按钮,您也可以访问单选的其他属性。比如
会给你单选按钮的id。
Ideally, you'd have a onclick assigned to the big table rather than each and every radio button. Events in JavaScript bubble up so the table (which is the eventual parent of all these radio buttons) will receiving the event.
So in jQuery you would have code like this
The signature of your handleOnClickRadio function would be
evt.target will identify the actual radio button that was clicked/checked and you can access other attributes of the radio as well. such as
Will give you the id of the radio button.
jQuery:
jQuery:
我根本不会将点击处理程序附加到按钮上。你说你有很多。将相同的事件处理程序附加到每个事件处理程序会浪费内存,甚至可能会出现性能问题。
使用事件委托代替:
然后你可以提取
i
和j
通过正则表达式(您也可以考虑更改模式,以便可以使用更简单的内容,例如split()
):您可以将其放在一起,如下所示:
编辑:< /em> 让代码变得更简单。
如果
i
和j
对应于列和行索引,请参阅 @Caspar Kleijne 的答案作为替代方案检索它们的方法。为了可访问性,您应该考虑将处理程序绑定到
change
事件。然后通过键盘进行的更改也将被识别。I would not attach the click handler to the buttons at all. You say you have lots of them. Attaching the same event handler to each of them is a waste of memory and could even be a performance problem.
Use event delegation instead:
Then you could extract the
i
andj
via regular expression (you could also consider to change the pattern so that you can use something simpler likesplit()
):You could put this together like so:
edit: Made code a bit simpler.
If
i
andj
correspond to column and row indicies, see @Caspar Kleijne's answer for an alternative way to retrieve them.For accessibility, you should consider binding the handler to the
change
event. Then changes via the keyboard will be recognized too.像这样连接事件
并使用像这样的处理程序
wire up the event like this
and use the handler like
您可以使用简单的 jQuery 将 onClick 方法附加到表中的单选按钮集合。当您说“名为“bigtable”的表”时,我假设您的意思是它在以下代码中具有 id="bigtable"。
然而,我通常会使用 class="magicRadioButton" 为每个单选按钮指定一个特定的类,然后您的 jQuery 代码会变得更加清晰,并且不依赖于表中的所有这些单选按钮;
现在,如果您需要将此信息插入到当前的 handleOnClickRadio 方法中,您可以使用以下命令来执行此操作。
You can attach an onClick method to a collection of radio buttons within a table with a simple bit of jQuery. When you say 'table called "bigtable"', I'm assuming that you mean that it has id="bigtable" in the following code.
However, I would usually give each of the radio buttons a specific class using class="magicRadioButton" and then your jQuery code becomes a little clearer and doesn't rely on all of those radio buttons being within a table;
Now, if you need to then plug this information into your current handleOnClickRadio method, you can do so with the following.
在点击触发器中将类名与
$(this)
结合起来Give them class names in conjunction with
$(this)
in your click trigger