【问题标题】:What is the opposite of !!this.checked?!!this.checked 的反义词是什么?
【发布时间】:2023-03-07 21:40:01
【问题描述】:

我试图取消选中另一个盒子。除了我需要知道this.checked 的反义词以及!!this.checked 的反义词之外,我一切正常。

基本上我想使用我现有的方法,而不是为新方法更改所有代码,所以我只需要知道相反的情况。

this.checked 选中复选框,但我想要相反的,所以当它运行时取消选中它。

【问题讨论】:

  • 能否贴出你的相关代码部分
  • 不确定我是否理解。布尔值 this.checked 的反面是 !this.checked。如果您想通过 JavaScript 取消选中某个框,您可以随时尝试this.checked = false; 另外,不确定您需要什么,但单选按钮可以在这里使用吗?这样,当另一个被选中时,一个取消选中。
  • 没关系,其他几个人已经在下面发布了答案,谢谢。

标签: javascript jquery html checkbox


【解决方案1】:

使用!! 两次基本上会将其后面的任何值转换为布尔值(truefalse),如果它还不是布尔值。

如果你有:

var example = 0;

然后调用:

console.log( !!example );

您将获得false,因为0false 作为布尔值。

当你使用! 一次时,你得到一个布尔值,但它的值是相反的,所以再次使用example 变量:

console.log( !example );

这将记录true,因为作为布尔值的0 的反面是true


所以,!!this.checked 的反面是 !this.checked,在您的场景中是 true(因为 this.checked 最初是 false


也许这会帮助你了解更多:

var foo = false,
    bar = true;

!foo; // true: opposite of false is true.
!bar; // false: opposite of true is false.

!!foo; // false: opposite of false is true, opposite of true is false.
!!bar; // true: opposite of true is false, opposite of false is true.

【讨论】:

  • 嗯??似乎没有回答这个问题。
  • 如果这不能回答您的问题或给您提供解决方法的想法,那么我不确定要告诉您什么,抱歉。 :L
【解决方案2】:

!!返回一个 not not。 说你有一个var test = true;

!test 变为 false

!!test 再次变为 true

记住它 - 砰!砰!你是布尔值。

【讨论】:

  • 谢谢!你是第二个,但这也回答了我的问题!谢谢! (CupawnTae was first)
【解决方案3】:

!!this.checked 的反义词就是!this.checked,即

chrome.storage.local.set({'theIDofmyCheckbox2': !this.checked});

解释:

!this.checkedtrue,而 this.checkedfalse 或任何其他虚假值(如 0null...),否则为 false

!!this.checked 与此相反。

【讨论】:

  • 谢谢!!完美,工作! :D 我会尽快接受,一旦我有 15 个代表我就会投票,这样我就可以了。
【解决方案4】:

试试这个是否被选中:

chrome.storage.local.set({'theIDofmyCheckbox2': $(this).is(':checked')});

Try this for is not checked:

chrome.storage.local.set({'theIDofmyCheckbox2': !$(this).is(':checked')});

! = 不, !! = 不是 不是 = 是

【讨论】:

  • 嗯,有趣但并非完全相反。其他人有更好的答案,但谢谢你,因为这很有用。
  • 显然我不可能是投票给你的人,因为你需要 125 个代表才能做到这一点,所以如果这就是你投票反对的原因,如果你删除它,我将不胜感激。
猜你喜欢
  • 2011-01-04
  • 2014-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多