If conditions false then prevent default

后端 未结 2 1398
轮回少年
轮回少年 2021-01-17 16:09

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.

2条回答
  •  隐瞒了意图╮
    2021-01-17 17:09

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

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

    UPDATE

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

提交回复
热议问题