If conditions false then prevent default(如果条件为假,则阻止默认)
问题描述
我有一个链接.当有人点击它时,我想在让它工作之前检查一些条件.如果它是 false
,则应该阻止默认操作.
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.
$(".pager-next a.active").click(function(event) {
if (!a == 1) {
event.preventDefault();
}
});
只有当 a
等于 1
时,该链接才有效.上面的代码是否正确.如果满足特定条件,则 a
设置为 1
.该链接只有在满足条件时才有效.
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.
推荐答案
假设 'should only work if a is equal to 1' 你的意思是 a
元素等于 1,试试这个:
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:
$(".pager-next a.active").click(function(event) {
if ($(this).text() != "1") {
event.preventDefault();
}
});
您可以修改 text()
以使用 jQuery 中可用的元素属性.
You can amend text()
to use whichever attribute of the element is available to you in jQuery.
更新
my a 是一个 var,在满足条件之前保持值为 0.
my a is a var which hold the value 0 until a condition is met.
在这种情况下,问题只是你的相等运算符不正确:
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();
}
});
这篇关于如果条件为假,则阻止默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果条件为假,则阻止默认


- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 失败的 Canvas 360 jquery 插件 2022-01-01