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

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

问题描述

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

推荐答案

假设'仅在a等于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.

更新


我的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();
    }            
});

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

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