在第一次表单验证javascript后移动到第二个表单

时间:2016-08-01 13:51:42

标签: javascript jquery

我不确定我的代码有什么问题。我在jquery mobile中使用两个表单,在第一个验证时,当点击锚标签时,如果验证成功,我想转到#page2上的表单2,或者如果验证失败,我想继续当前页面。问题是如果第一个表单上的所有数据都是正确的,我可以导航到第二个表单,但如果它不正确,event.preventDefault()会被触发,如果我填写正确的表单,它不会让我去表格2。

<form action="#" method="post" data-ajax="false">

<span class="error_form" id="username_error"></span>
<label for="username">Username : <span></span></label>
<input type="text" name="username" id="username" placeholder="Enter username"     value="">

<span class="error_form" id="password_error"></span>
<label for="password">Create password : <span></span></label>
<input type="password" name="password" id="password" placeholder="Enter password">

<span class="error_form" id="password_error_confirmation"></span>
<label for="passwordAgain">Confirm password : <span></span></label>
<input type="password" name="password_confirmation" id="password_confirmation" placeholder="Enter password again">

<div class="ui-grid-a">
            <div class="ui-block-a">
<a href="#" data-role="button" data-icon="arrow-l" data-iconpos="left" data-transition="pop">Back</a>
</div>
            <div class="ui-block-b">
<a href="#page3"  id="register1" data-role="button" data-icon="arrow-r" data- iconpos="right" data-transition="pop">Next</a>
</div>
        </div>


</form>


script.js, I have tried it with event.PreventDefault()
and it doent work

$('a#register1').click(function(event){ 

    check_username();
    check_password();
    check_password_confirmation();


    if(error_username == false && error_password == false && error_password_confirmation == false){
        sessionStorage.setItem("username", $("#username").val());
        sessionStorage.setItem("password", $("#password").val());
        sessionStorage.setItem("password_confirmation", $("#password_confirmation").val()); 

        $(this).attr("href", "#page3");  

    }else{

        $(this).removeAttr("href");


    }

});

2 个答案:

答案 0 :(得分:0)

  $('a#register1').click(function(event){ 


    check_username();
    check_password();
    check_password_confirmation();


    if(error_username == false && error_password == false &&      error_password_confirmation == false){
        sessionStorage.setItem("username", $("#username").val());
        sessionStorage.setItem("password", $("#password").val());
        sessionStorage.setItem("password_confirmation", $("#password_confirmation").val()); 
        $('a#register1').attr("http://depressionapp1.westeurope.cloudapp.azure.com/register.html#page3");

        //window.location.href="http://depressionapp1.westeurope.cloudapp.azure.com/register.html#page3";  
    }else{
        $('a#register1').removeAttr("href");
    }

});
 //This is my new code but it still does not direct me to page3

答案 1 :(得分:0)

$(&#39;一个寄存器1#&#39)。单击(函数(){

    $('a#register1').attr("href", "http://depressionapp1.westeurope.cloudapp.azure.com/register.html#page3");
    error_username = false;
    error_password = false;
    error_password_confirmation = false; 

    check_username();
    check_password();
    check_password_confirmation();


    if(error_username == false && error_password == false && error_password_confirmation == false){
        sessionStorage.setItem("username", $("#username").val());
        sessionStorage.setItem("password", $("#password").val());
        sessionStorage.setItem("password_confirmation", $("#password_confirmation").val()); 



    }else{
        $('a#register1').removeAttr("href");

    }



});