簡體   English   中英

jQuery開/關單擊

[英]jQuery on/off click

我需要攔截頁面上的點擊並執行一些驗證邏輯,然后繼續進行操作。

我有一個鏈接:

<a data-link="true">Link</a>

js攔截:

$("a[data-link='true'").off("click.link").on("click.link", (event) => this.validate(event));

在驗證功能中,我只是進行一些驗證,我需要繼續進行該請求。

如何截獲點擊,如果可以,請繼續原始請求?

謝謝。

如果將data選擇器概括化,它可能會做得更好:

$(function(){
    // Listen for data link in general
    $('a[data-link]').on('click',function(e){
        // Stop all request
        e.preventDefault();
        // Get the link href
        var hrefPath    =   $(this).attr('href');
        // If the data-link is set to true
        if($(this).data('link') === true) {
            // Do your validation here, not sure if your validation function
            // works or not, but here is where you apply it
            var valid   =   true;
            // If failed, set the default path to empty
            if(!valid)
                hrefPath = false;
            // If the path is not empty, go to the default path
            if(hrefPath !== false)
                window.location =   hrefPath;
            // Stop
            return false;
        }
        // If the link is not true, just go to the default path
        window.location =   hrefPath;
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM