简体   繁体   中英

Anchor tag issue - href and click event

I have anchor link with click and href events. I need to run click event first then once complete the event it should call href to access action class. I have updated the sample code in jsfiddle like complete the click event , it should forward to stackoverflow.com. But its not forwarding after click.

Please advise.

$(document).ready(function () {
    $("div.actions").append('<a id="excelExport" class="actionButton" alt="Export to Excel" title="Export to Excel" href="listexport">click me</a>');
    $('div.actions').on('click', '#excelExport', function (e) {
        e.preventDefault();
        callajax();
    });
});

function callajax() {
    jQuery.ajax({
 url : '',
     data : 

}

JSFIDDLE

Since you prevent the link, you need to send this.href to the CallAjax function and do location=href in the success method

This is of course assuming you'll change the alert to an Ajax call later

demo

    callajax(this.href);


function callajax(href) {
    var URL = href;
    $.get("something",{"URL":URL },function(URL){
    // url is returned from server
    location.href=URL;
  });
}

OR

function callajax(href) {
    var URL = href;
    $.get("something",{"URL":URL },function() { // reuse url passed to function
    location.href=URL;
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM