【问题标题】:Anchor tag issue - href and click event锚标记问题 - href 和点击事件
【发布时间】:2013-07-14 04:20:55
【问题描述】:

我有带有 click 和 href 事件的锚链接。我需要先运行点击事件,然后一旦完成事件,它应该调用 href 来访问动作类。我已经更新了 jsfiddle 中的示例代码,比如完成点击事件,它应该转发到 stackoverflow.com。但点击后不转发。

请指教。

$(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

【问题讨论】:

  • 那是因为你使用的是e.preventDefault()如果调用该方法,则不会触发事件的默认动作。

标签: javascript jquery


【解决方案1】:

由于你阻止了链接,你需要将this.href发送到CallAjax函数,并在success方法中做location=href

这当然是假设您稍后将警报更改为 Ajax 调用

demo

    callajax(this.href);


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

或

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

【讨论】:

  • ajax 调用后,href 动作用于访问动作类方法。所以访问方法也可以使用 location.href 吗?
  • 在我的示例中,我再次从服务器返回 href,您不必
猜你喜欢
  • 1970-01-01
  • 2019-10-21
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 2014-12-13
相关资源
最近更新 更多