In jQuery, why does programmatically triggering 'click()' on a checkbox not immediately check it?

徘徊边缘 提交于 2019-11-29 13:19:53

There are three types of actions going on here:

  1. Manually clicking it changes the state. That is all it does by itself; the trigger then fires because it is realizing that it has been clicked. This is a matter of browser behavior before scripting behavior.
  2. Calling the Click() event calls the javascript event first. jQuery prioritizes the function you provided, as it may very well cancel that click or perform some other action. After that is completed, javascript changes the status of the checkbox.
  3. Lastly the Change() event is mimicking the manual clicking by telling the browser to click it. That means that the item is being changed by the browser, which then in turn is calling your triggered event.

Essentially the difference is that the Click() is a straight javascript call. It can do things the others cannot, such as cancel the change and do all sorts of other zany stuff, so it is delaying the change of state for as long as it can.

The click event fires before the default action. It does this to allow you to prevent the default action from occurring by returning false from the handler.

In your example, this means that you alert the old value as the default action of changing the checkbox state has not yet occurred.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!