jQuery-e.preventDefault()之后,trigger(“ click”)无法正常工作

瓦德瓦

我正尝试在浏览器将用户重定向到http://example.com点击之前执行异步操作

因此,在ajax调用之后,我再次单击currentTarget进行重定向,但是它不起作用。

<a class="notic_click" href="http://example.com">Button</a>
<a class="notic_click" href="http://example.com">Button</a>


$(".notic_click").on("click", function(e, options) {
       options = options || {};
      
       // if it hasn't been clicked already
       // run this
       if( !options.hasClicked ) {
            e.preventDefault();

            const self = this;
          
            // some other code

            $.get("http://example.com/hello")
                 .always(function() {
                     $(self).trigger("click", {hasClicked: true});
               });
        } else {
            // I got here, that means e.preventDefault didn't get called and it should redirect
            console.log("should redirect but not redirecting");
        }
 });

$(self).off()在扳机之前尝试过,但无济于事。

我已经尝试过了:

window.location.assign(e.target.href);

这可行。但是我很好奇是什么原因导致上述方法不起作用。

浏览器:Mozilla Firefox(78.0.2)

罗里·麦克罗森(Rory McCrossan)

您的代码看起来做工精细的隔离话虽这么说,一种更好的方法是始终调用preventDefault()来停止页面重定向,然后发出异步AJAX请求。该请求完成后,您可以window.location.assign()用来重定向用户。

$(".notic_click").on("click", e => {
  e.preventDefault();

  // I'd suggest putting a loading spinner in the UI here to make it clear
  // to the user that their request is in process.

  $.get("http://example.com/hello").always(() => {
    window.location.assign(e.target.href);
  });
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

jQuery .click()无法正常工作

来自分类Dev

jQuery click()无法正常工作

来自分类Dev

jQuery click()无法正常工作

来自分类Dev

jQuery表单验证-preventDefault之后无法提交

来自分类Dev

<span> jQuery .click()无法正常工作

来自分类Dev

.click在jquery上无法正常工作

来自分类Dev

jQuery .click()无法正常工作(调试)

来自分类Dev

Click for CSS上的JQuery无法正常工作

来自分类Dev

.click在jquery上无法正常工作

来自分类Dev

jQuery Post之后,PHP isset无法正常工作

来自分类Dev

外部jquery .click()事件有时无法正常工作

来自分类Dev

jQuery .on()无法正常工作

来自分类Dev

jQuery .on()无法正常工作

来自分类Dev

jQuery $('a')无法正常工作

来自分类Dev

Struts2 / jQuery:在e.preventDefault()之后恢复链接href重定向;

来自分类Dev

jQuery e.preventDefault(); 不能按预期工作

来自分类Dev

jQuery无法正常工作。jQuery的

来自分类Dev

bpopup jQuery无法正常工作

来自分类Dev

jQuery冲突无法正常工作

来自分类Dev

jQuery Valildator无法正常工作

来自分类Dev

.css()jQuery无法正常工作

来自分类Dev

jQuery tinysort无法正常工作

来自分类Dev

jQuery Fancybox无法正常工作

来自分类Dev

jQuery Datepicker无法正常工作

来自分类Dev

jQuery .slideUp()无法正常工作

来自分类Dev

jQuery .next()无法正常工作

来自分类Dev

jQuery .data()无法正常工作?

来自分类Dev

jQuery $ .when()无法正常工作

来自分类Dev

jQuery remove()无法正常工作