如果条件未重定向到另一个页面,则使用JavaScript

时间:2019-08-09 19:48:34

标签: javascript html button onclick href

我有一个按钮,当您单击它时,它将启动一个函数,该函数的目的是查看一堆语句是否为真,如果是,则应将我发送到另一页

    <a href="">
                <button value="Registar" id="btn_registar" type="button" class="btn btn-default text-center" style="width: 90px; height: 30px; border-radius: 4px; cursor: pointer;" onclick="validar()">
                    <b>Registar</b></button>
            </a> <p id="reg_off></p>

我的功能:

        function validar(){

        var nome = document.getElementById("nome").value;
        var username = document.getElementById("username").value;
        var password = document.getElementById("password").value;
        var conf_password = document.getElementById("conf_password").value;
        //var idade = .... FALTA O CALCULO DA IDADE

        if(nome == "" || username == "" || password == "" || conf_password == "") 
        {
            //alert("NOT WORKING");
            document.getElementById("reg_off").innerHTML = "ups";
            //WHEN I USE THE BUTTON ON THIS CONDITIONS, THE MESSAGE APPEARS BUT IT ONLY SHOWS FOR LIKE A MICROSECOND
        } else {
            alert("WORKED");
            window.location.href = "area_reservada.php";    
        }
    }

1 个答案:

答案 0 :(得分:-1)

将其更改为

window.location.href = "area_reservada.php";

应该帮助

(但这会将URL设置为area_reservada.php),因此您需要在其中插入完整的URL