dongqi6964 2015-03-06 18:10
浏览 160
已采纳

两个表单之间的html jquery切换[关闭]

I have two forms on a single html page A login form and a Register form

I want to be able to toggle between the forms when i click on the coressponding "span" button

The page should load with just the login form showing but both the login and register "span" buttons at the top

When i click login i want the login form to "hide" and the register form to be shown and vice versa.

here is the code i have so far:

          <!DOCTYPE html>
    <head>
        <title>Login Form</title> 
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    </head>
    <body>

        <span href="#" class="btn btn-default" id="toggle-login">Log in</span> 
        <span href="#" class="btn btn-default" id="toggle-register">Register</span>

            <div id="loginForm">

                <form id='login' action='' method='post' accept-charset='UTF-8'>
                <fieldset>
                    <legend>Log In</legend>
                    <p><input type="text" name="login" value="" placeholder="studentID"></p>
                    <p><input type="password" name="password" value="" placeholder="Password"></p>
                    <p class="submit"><input type="submit" name="commit" value="Login"></p>
                    </fieldset>
                </form>
            </div>

        <div id="registerForm">
    <form id='register' action='' method='post' accept-charset='UTF-8'>
        <fieldset >
            <legend>Register</legend>
                <input type='hidden' name='submitted' id='submitted' value='1'/>
                <p><label for='username' >UserName*:</label> <input type='text' name='username' id='username' maxlength="50" /></p>
                <p><label for='name'>First Name*: </label> <input type='text' name='name' id='firstName' maxlength="50" /></p>
                <p><label for='name'>Second Name*: </label> <input type='text' name='name' id='secondNname' maxlength="50" /></p>
                <p><label for='email' >Email Address*:</label> <input type='text' name='email' id='email' maxlength="50" /></p>
                <p><label for='password' >Password*:</label> <input type='password' name='password' id='password' maxlength="50" /></p>
                <input type='submit' name='Submit' value='Submit' />
                </fieldset>
                </form>
        </div>


        <script>

        </script> 
    </body>
</html>

Thanks!

  • 写回答

1条回答 默认 最新

  • douyan1927 2015-03-06 20:17
    关注

    useExtending from Erick Souza's answer. You can also add a 'formnovalidate' attribute to the form you don't want to validate. This will cause the one form to validate on submit and not the other. It has been my experience that you can not simply hide one form and validate the other. As the submit button will attempt to validate both forms.

    This idea might look like:

    $("#toggle-login").click(function(){
        $("#register").hide().attr("formnovalidate");
        $("#login").toggle();
    });
    
    $("#toggle-register").click(function(){
        $("#login").hide().attr("formnovalidate");
        $("#register").toggle();
    });
    

    To force an elements behavior on page load you should use this basic handler:

    $("document").ready(function(){
        $("#register").hide();
    };
    

    That should do the trick. Or in your css doc you can use the attribute display: none;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?