【问题标题】:window.location.href with $(".confirm").click(function (e) does not return full urlwindow.location.href 与 $(".confirm").click(function (e) 不返回完整的 url
【发布时间】:2015-01-26 20:20:39
【问题描述】:

我无法确定以下代码的问题所在。 我的完整网址是 http://localhost:4244/Invoice/PayInvoice?invoicenumber=7069 但 var url = window.location.href;只返回http://localhost:4244/

<a Class="btn btn-success btn-sm confirm" href="/Invoice/PayInvoice?invoicenumber=7069">Pay Invoice</a>

谁能看出我做错了什么

<script>
    $(document).ready(function() {
        $(".confirm").click(function (e) {
            var url = window.location.href;     // Returns full URL
            alert(url);
            $("#displayPopUp").show();

            e.preventDefault();

            $(".yesupdate").click(function () {
                $("#displayPopUp").hide();

                window.location = url;
                //$(location).attr('href');
            });
            //if (AcceptPayment()) {
            //    // then redirect to original location
            //    window.location = this.href;
            //}
            //else {
            //    alert("Couldn't do my thing first");
            //}
            //var result = window.confirm("You are about to mark invoice as paid, are you sure?");
            //if (result == false) {
            //    e.preventDefault();
            //}
        });
    });
</script>

我想要做的是将 url 的值传递给 window.location = url;当 .yesupdate 被点击时

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    如果您尝试导航到被点击的&lt;a&gt; 元素的href,您可以使用以下方法在.click() 处理程序中检索该地址:

    var url = this.href;
    

    你的 sn-p 中的行只是将当前的location 分配回它自己,这只会重新加载页面。

    var url = window.location.href;
    
    window.location = url;
    

    您可能还需要从 $(".yesupdate") 中删除以前的 click 处理程序,以避免它们从多个 .confirms 中堆叠。

    您可以使用namespaces 来定位某些处理程序。

    $('.yesupdate').off('.confirm').on('click.confirm', function (e) {
        // ...
    });
    

    【讨论】:

      【解决方案2】:

      你不应该分配给window.location,它是a read-only instance of a Location object。根据您的浏览器,这可能会或可能不会工作或抛出。

      相反,您应该使用您想要导航到的 URL 调用 window.location.assign(url)。您可以使用带有查询字符串和其他部分的完整 URL。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-30
        • 1970-01-01
        • 2017-09-27
        相关资源
        最近更新 更多