Javascript onchange问​​题 [英] Javascript onchange question

查看:87
本文介绍了Javascript onchange问​​题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有几个复选框,如下所示:

If I have a few checkboxes like the following:

<input type="checkbox" onchange="foo(bar);" /> Bar

仅当通过单击或按键(我想要的)选中/选中复选框时,或者如果我通过选择/取消选择alll按钮以编程方式选中/取消选中许多复选框时,才会调用foo(bar);函数然后为所有复选框(我不需要的)调用onchange函数?

Will the foo(bar); function only be called when the checkbox is checked on/off by a click or keypress (what I want) or if I progromatically check/uncheck a lot of checkboxes with a select/unselect alll button, will it then call the onchange function for all of the checkboxes (what I don't want)?

所有浏览器的行为都一样吗?

And will all browsers have the same behavior?

推荐答案

Internet Explorer(至少是旧版本)在复选框失去焦点之前不会触发更改"事件.尝试改用"onclick".

Internet Explorer (at least the old ones) doesn't fire the "change" event until the checkbox loses focus. Try using "onclick" instead.

如果您的代码更改了已检查"状态,则不会调用该回调函数(,除非Bouman先生的回答描述了这种情况)

The callback won't be called if your code changes the "checked" status ([edit] except in cases as described by Mr. Bouman's answer)

这篇关于Javascript onchange问​​题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆