javascript 如果条件为假,则防止默认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9376089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:28:10  来源:igfitidea点击:

If conditions false then prevent default

javascriptjqueryconditionalpreventdefault

提问by esafwan

I have a link. When some one clicks on that I want to check some conditions before letting it work. If it's falsethe default action should be prevented.

我有一个链接。当有人点击它时,我想在让它工作之前检查一些条件。如果false是默认操作,则应阻止。

$(".pager-next a.active").click(function(event) {
    if (!a == 1) {
        event.preventDefault();
    }           
});

The link should only work if ais equal to 1. Is the above code correct. ais set to 1if a particular condition is met. The link should only work if the condition is met.

该链接仅在a等于时才有效1。上面的代码是否正确。如果满足特定条件,a则设置为1。该链接仅在满足条件时才有效。

回答by Rory McCrossan

Assuming by 'should only work if a is equal to 1' you mean the text of the aelement is equal to 1, try this:

假设 '应该只在 a 等于 1 时起作用,你的意思是a元素的文本等于 1,试试这个:

$(".pager-next a.active").click(function(event) {
    if ($(this).text() != "1") {
        event.preventDefault();
    }           
});

You can amend text()to use whichever attribute of the element is available to you in jQuery.

您可以修改text()以使用 jQuery 中可用的元素的任何属性。

UPDATE

更新

my a is a var which hold the value 0 until a condition is met.

我的 a 是一个 var ,它保持值 0 直到满足条件。

In which case, the problem was simply that your equality operator was incorrect:

在这种情况下,问题只是您的相等运算符不正确:

$(".pager-next a.active").click(function(event) {
    if (a != 1) {
        event.preventDefault();
    }            
});

回答by Thomas Clayson

Be careful:

当心:

!aevaluates to true or false. If a conversion of ato a bool is truethen !aevaluates to false.

!a评估为真或假。如果转换为abooltrue!a评估为 false。

All positive integers evaluate to true. So !awill evaluate to false. A comparison using double equals ==to 1 will test that boolean !awith the boolean 1or true. So if ais a positive integer as I suspect it is then your ifstatement will ALWAYS evaluate to false.

所有正整数的计算结果为true。所以!a将评估为假。使用 double 等于==1 的比较将使用布尔!a1or测试该布尔值true。因此,如果a是我怀疑的正整数,那么您的if陈述将始终评估为假。

If you want to test is something is NOT something else you need to change the first equals in your comparison operator (===) to be a !.

如果您想测试某个东西不是别的东西,您需要将比较运算符 ( ===) 中的第一个等号更改为 a !

E.g. var a = 2; if(a!==1) { // do something }<-- A is 2 and therefore the if comparison wille evaluate to true as adoes notequal 1.

例如var a = 2; if(a!==1) { // do something }<-- A 是 2,因此 if 比较将评估为 truea等于1

In your code we have:

在您的代码中,我们有:

var a = 2;
if(!a==1){
  // a was 2 (or boolean true by default)
  // but using ! has negated its boolean value
  // so !a evaluates to boolean false
  // which is being compared to 1 (evaluating to boolean true)
  // so this if statement will never get here
}

Hope that helps

希望有帮助

P.S. Remember your comparison operators:

PS 记住你的比较运算符:

!"hello world" == 0 // true
!"hello world" === 0 // false

Update

更新

I saw your comment on another post which said that ais 0until something happens then it is 1.

我看到了另一个帖子里面说,您的评论a0,直到事情发生那么它是1

In this case:

在这种情况下:

var a = 0; // integer 0 or bool false
if(!a==1){ // if the bool opposite of 0 (false) is equal to 1 (true)
  // well, opposite of false is true, so you're checking if true is equal to true
  // so this will get called
  e.preventDefault();
}