Strange jQuery ("checkbox"). Click (). Is (": checked") behavior

Can anyone explain why $(this).is(":checked") gives the opposite result when clicking on a button using $("#test").click() than when manually clicking or using document.getElementById("test").click() ??

EDIT The requested behavior here - thanks:

http://jsfiddle.net/ub8Zk/4/

EDIT 2

This turned me on, but I finally realized - in version 1.5.2 jQuery, the event handler for the change event is triggered when the click() method is called (for example, native js) !! Not so in previous versions.

Look at here:

http://dl.dropbox.com/u/6996564/jquery_click_test/test-1.4.4.htm ... test-1.5.1.htm ... test-1.5.2.htm

Can someone help me report this error?

+4
source share
4 answers

The click event occurs BEFORE the value changes, so it gets the value old . The default click handler happens AFTER your click event and switches the value. That is why it gets the opposite meaning. I would have thought that the document click function does something strange (I would not trust it, I would trust jQuery).

Have a look at this script: http://jsfiddle.net/ub8Zk/4/

+5
source

Since you are using a checkbox, you want is(':checked') not is(':selected')

+3
source
 $('input#someCheckbox').click(function() { if ($(this).is(':checked')) { // checked } else { // not checked }; }); 
+2
source

I do not see your code. click () binds a mouse event handler according to the documentation. You can also write: $ ('# foo'). Bind ('click', function () {alert ("The user pressed" foo. ");}); If you want to call the trigger () function like this: $ ('foo'). Trigger ('click') or document .getElementById ('foo'). Checked = true; when using direct javascript.

-1
source

Source: https://habr.com/ru/post/1348184/


All Articles