如果条件为假,则阻止默认 [英] If conditions false then prevent default

查看:41
本文介绍了如果条件为假,则阻止默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接.当有人点击它时,我想在让它工作之前检查一些条件.如果它是 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();
    }            
});

这篇关于如果条件为假,则阻止默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆