简体   繁体   English

更改链接href onclick然后运行

[英]Change link href onclick then run it

I want to open a link to a new window without using window.open() from a link retrieving from an ajax call. 我想不使用从ajax调用检索的链接而使用window.open()来打开到新窗口的链接。

I have an hyperlink with href="#" . 我有一个带有href="#"的超链接。 I want to change the href then go to a link from an ajax call when the user click on the link once. 我想更改href,然后在用户单击链接时从ajax调用转到链接。

I am not trying to change the link after the event has been consumed. 事件耗尽后,我不尝试更改链接。

Here is my code: 这是我的代码:

<a href="#" id="preview-btn-link"><i class="fa fa-eye fa-2x" id="save-btn"></i></a>
<script>
                var lockLink = true;
                $("#preview-btn-link").click(function (e) {
                    if (lockLink) {
                        lockLink = false;
                        e.preventDefault();

                        var a = $(this);
                        saveProject("true", function (r) {
                            gvwcProject.id = r;

                            $.post("ajax_path", {"id": r, "pn": paj.non})
                                    .done(function (r) {
                                        console.log(r);
                                        a.attr("href", r);

                                        a.click();
                                    })
                                    .fail(function () {
                                        console.log("fail");
                                    });

                        }, function () {
                            console.log("failed");
                        });
                    } else {
                        lockLink = true;
                    }
                });
</script>

I can see the link from the href has been changed. 我可以看到href中的链接已更改。 But it does not call the link back even if I click on the link back. 但是,即使我单击链接,它也不会回调该链接。

Well, I just get it work by submiting a form. 好吧,我只是通过提交表格使它起作用。

<form id="form" method="post" action="the_link" target="_blank">
  --some fields --
</form>

js: JS:

$("#preview-btn-link").click(function (e) {
   //some check
   //Edit the form attributes then submit
   $("#form").submit();

   return false;
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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