简体   繁体   中英

i want button on which when click it first check the password if the password is abcd than shows the another submit button else show an alert

 <form action="#" method="post" target="_blank">
        <div id = "another sub">
        </br>
        <input type = "password" size = "25px" id="pswd">
        </br>
        </div>
        <button><a href="table.html" target="_blank" onclick="checkAddress('password');" >Submit</a></button>
<script>
        function checkAddress(field){
            var val = document.getElementById("pswd").value;
            if  (val === "abcd"){
                alert ("Matched");
            }
            else {
                alert("Try for Next Time");
            }
        }
        </script>
    </form>

i have a form here in which it first check password than takes to the another page

You have to Add your Script Tag below the form tag or you can add it at the end of the page before the body closing tag, make sure to include the js in the page

<form action="#" method="post" target="_blank">
    <div id = "another sub">
    </br>
    <input type = "password" size = "25px" id="pswd">
    </br>
    </div>
    <button onclick="checkAddress('password');">Submit</button>
</form>
 <script>
    function checkAddress(field){
        var val = document.getElementById("pswd").value;
        if  (val === "abcd"){
            alert ("Redirect To page if Matched Found");
            window.location = "http://www.redirecturlhere.com/"
        }
        else {
            alert("Try for Next Time");
        }
    }
    </script>

In your case, you don't need <form> and <a> tags. Just redirect when password is input correct.

 function checkAddress(field) { var val = document.getElementById("pswd").value; if (val === "abcd") { alert("Matched"); document.location.href = 'table.html'; } else { alert("Try for Next Time"); } } 
 <div id="another sub"> <input type="password" size="25px" id="pswd"> </div> <button onclick="checkAddress();">Submit</button> 

Don't use a form to avoid the reload:

 function checkAddress(field) { var val = document.getElementById("pswd").value; if (val == "abcd") { alert("Matched"); location.href = "table.html"; } else { alert("Try for Next Time"); } } 
 <div id="another sub"> <input type="password" size="25px" id="pswd"> </div> <button onclick="checkAddress()">Submit</button> 

It would be wise to not use inline js, using a examplename.js file that is loaded would be better solution.

 function checkAddress(field) { var val = document.getElementById("pswd").value; if (val === "abcd") { alert("Matched"); document.location.href = 'table.html'; } else { alert("Try for Next Time"); } } 
 <div id="another sub"> <input type="password" size="25px" id="pswd"> </div> <button onclick="checkAddress();">Submit</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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