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
If conditions false then prevent default
提问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 false
the default action should be prevented.
我有一个链接。当有人点击它时,我想在让它工作之前检查一些条件。如果false
是默认操作,则应阻止。
$(".pager-next a.active").click(function(event) {
if (!a == 1) {
event.preventDefault();
}
});
The link should only work if a
is equal to 1
. Is the above code correct. a
is set to 1
if 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 a
element 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:
当心:
!a
evaluates to true or false. If a conversion of a
to a bool is true
then !a
evaluates to false.
!a
评估为真或假。如果转换为a
booltrue
则!a
评估为 false。
All positive integers evaluate to true
. So !a
will evaluate to false. A comparison using double equals ==
to 1 will test that boolean !a
with the boolean 1
or true
. So if a
is a positive integer as I suspect it is then your if
statement will ALWAYS evaluate to false.
所有正整数的计算结果为true
。所以!a
将评估为假。使用 double 等于==
1 的比较将使用布尔!a
值1
or测试该布尔值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 a
does 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 a
is 0
until something happens then it is 1
.
我看到了另一个帖子里面说,您的评论a
是0
,直到事情发生那么它是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();
}