Home>

When I press the check box multiple times, I want to put characters on the console according to the state of the check box.
On the first click, the console display seems to work fine, but after the second time, the console value is the same as the first time, and it seems that it does not work well.

What kind of code should I write in these cases?

HTML

<input type = "checkbox" name = "selector" data-category = "15" checked>

jQuery

$('# s-option'). click (function () {
     // If it is not checked
     if ($('input [name = "selector"]'). prop ('checked', false)) {
  console.log ('Not checked');
     // if unchecked
     } else {
       console.log ('Checked');
     }
   });
  • Answer # 1

    Is the judgment method below?

    if ($('input [name = "selector"]'). prop ('checked', false)) {
    ↓
    if ($("# s-option"). prop ("checked") == false) {

  • Answer # 2

    if ($(' input [name = "selector"] '). prop (' checked ', false)) {
    ↓↓↓
    if ($('input [name = "selector"]'). prop ('checked') == false) {