【问题标题】:window.location.replace(); is not redirecting the page on button clickwindow.location.replace();没有在按钮单击时重定向页面
【发布时间】:2021-10-24 11:39:58
【问题描述】:

我试图在单击按钮时重定向页面,但是当我单击按钮时它只刷新页面并且我停留在同一页面

我也试过这个:

window.location = "https://stackoverflow.com/";

当前 jquery:

<script>
        $(".ola").click(function () {
            var bl = $(this).attr('id');
            alert(bl);
            window.location.replace("menu.aspx?Pay=" + bl);

        });
    </script>

按钮:

divNaoPago.InnerHtml += "<td><Button type=\"submit\" id=\"" + BL4 + "\" class=\"ola\" style=\"cursor: pointer;\">Pay</Button></td>";

【问题讨论】:

    标签: c# jquery asp.net webforms


    【解决方案1】:

    尝试像这样修改您的代码:

      $(".ola").click(function (event) {
        event.preventDefault();
        var bl = $(this).attr('id');
        alert(bl);
        window.location.replace("menu.aspx?Pay=" + bl);
      });
    

    【讨论】:

    • 为什么会这样?我已经使用了window.replace,就像我展示的那样,它工作得很好,为什么它现在不能工作,为什么(event)event.preventDefault();,对不起,我是jquery的新手
    • @MiguelAraujo w3schools.com/jsref/obj_event.asp,在这里你可以找到一个很好的解释。
    • 您有一个submit 类型的按钮,该按钮的默认操作是发布到您的表单中指定的端点。如果您希望 js 控制您需要阻止首先出现的默认操作。
    【解决方案2】:

    window.location.replace("menu.aspx?Pay=" + bl);

    试试这个 window.location.href("menu.aspx?Pay=" + bl);

    【讨论】:

    • 我尝试输入window.location.href("menu.aspx?Pay=" + bl);,它仍然是相同的输出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2021-11-23
    • 1970-01-01
    • 2021-09-07
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多