Javascript只是没有重定向,而是重新加载index.html

时间:2015-01-18 12:25:13

标签: javascript html

所以,我正在尝试制作一个小的虚拟登录表单,当我点击一个按钮时,会将我重定向到另一个页面。 但它只是没有,而是再次重新加载index.html。

HTML(编辑):

<div class="modal fade" id="login" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <form action="" class="form-horizontal">
                    <div class="modal-header">
                        <h4>Log In</h4>
                    </div> <!-- End of Modal Header -->
                    <div class="modal-body">

                        <div class="form-group">

                            <label for="login-name" class="col-lg-2 control-label">Username</label>
                            <div class="col-lg-10">
                                <input type="text" placeholder="Enter Your Username" class="form-control" id="login-name">
                            </div>
                        </div> <!-- End of -->

                        <div class="form-group">

                            <label for="login-name" class="col-lg-2 control-label">Password</label>
                            <div class="col-lg-10">
                                <input type="password" placeholder="Enter Your Password" class="form-control" id="login-name">
                            </div>
                        </div> <!-- End of -->
                        <div class="form-group">

                            <button class="btn btn-default pull-right btn-large" id="loginBtn" style="margin-right: 20px;">Login</button>

                        </div> <!-- End of -->

                    </div> <!-- End of Modal Body -->
                    <div class="modal-footer">
                        <a href="#" class="btn btn-default btn-xs">Forgot Password</a>
                        <a href="#" class="btn btn-default btn-xs">Sign Up</a>
                        <a href="#" class="btn btn-default btn-xs" data-dismiss="modal">Close</a>
                    </div> <!-- End of Modal footer -->
                </form>
            </div> <!-- End of Modal Content -->
        </div> <!-- End of Modal Dialog -->
    </div> <!-- End of Modal Fade LOGIN -->

JS:

window.addEventListener("load",init, false);

function init() {
    var loginBtn = document.getElementById("loginBtn");
    loginBtn.addEventListener("click", checkLogin, false);
}

function checkLogin(e) {
    alert("FIRED");
    if(1>0){
        window.location.href = "//www.google.com";
    }
}

起初我认为它可能没有重定向,因为它根本没有调用该功能,但确实如此。每次都会触发警报,但之后只会重新加载页面。

当我告诉它在init函数中执行它时,它也可以重新定位。我哪里错了?

2 个答案:

答案 0 :(得分:0)

<button>元素的默认行为是执行提交操作。由于您没有取消它,因此表单正在提交,而您对location.href所做的更改将被忽略。由于您尚未为action=""指定值,因此提交只会导致当前页面刷新。

三种不同的方法可以避免这种情况:

  1. 在按钮上指定type="button"
  2. <button class="btn btn-default pull-right btn-large" 
            id="loginBtn" style="margin-right: 20px;" type="button">Login</button>
    
    1. inputtype="button"
    2. 一起使用
      <input class="btn btn-default pull-right btn-large" value="Login"
              id="loginBtn" style="margin-right: 20px;" type="button" />
      
      1. 防止事件处理程序中的默认行为:
      2. function checkLogin(e) {
            e.preventDefault();
        
            alert("FIRED");
            if(1>0){
                window.location.href = "//www.google.com";
            }
        }
        

答案 1 :(得分:0)

您还可以使用更简单的方法,我更喜欢按钮标记中的window.location.href,如下所示:

<button class="Whatever you want to call it" onclick="window.location.href='The_Page_You_Want_To_Redirect_To.html'"></button>

然后您可以根据需要使用CSS设置按钮的样式