0

我试图向我编写的 javascript/jquery 脚本添加第二个表单,但是,当我实现并开始测试它时,当我尝试通过 jquery post 请求发送固定表单时,它会发送一个 get 请求. 我知道这与第二种形式有关,因为为第二种形式注释掉脚本会使站点再次工作。希望一双新鲜的眼睛可以提供帮助!(加载页面功能也可以)

注册功能似乎是问题所在:

  //when the user first goes to the page
  $(document).ready(function(){ 
        var user = $("#username");
        var pass = $("#password");
        var submit = $("#submit");

            //both of these methods work, I'm not sure if one is better than the other
    //$("#submitbutton").click(function(event){
    $("#loginform").on('submit', function(event){
    event.preventDefault();
    $('.loginerror').remove();
    $.post("login.php", {username:user.val(), password:pass.val()}, function(data){

        if(!data)
        {
            $("#login").append("<h3 class='loginerror'>Incorrect Username or Password</h3>");

        }

        else
        {
        loadpage();
        }

    }, "json");

});

//if the user clicks the username prompt him with the login div
$("#registerbutton").click(function(event){
    register();
});

});

function register()
{
$("#login").hide("slow");
$("#registerdiv").css("display", "block");

//initiate some variables
var regusername = $("#regusername");
var reg1password = $("#reg1password");
var reg2password = $("#reg2password");
var regemail = $("#regemail");
var regfirstname = $("#regfirstname");
var reglastname = $("#reglastname");

//TODO: check to make sure form is filled out properly
$("#registerform").on('submit', function(event){

    event.preventDefault();
    //send post request
    $.post("register.php", {regusername:regusername.val(), password1:reg1password.val(), password2:reg2password.val(), email:regemail.val(), firstname:regfirstname.val(), lastname:reglastname.val()}, function(data){
        if(!data)
            $("#registerdiv").append("<h3> class='loginerror'>Server error, retry</h3>");
        else
            $("#registerdiv").hide("slow");
            $("#loginiv").slidedown("slow");
    }, "json");

}

这是具有两种形式的 HTML:

<body>

<Div id="welcome"> Welcome </div>
<Div id="login">
    <br><br><h2>Welcome to QuotesLib</h2>
    <br><br><br><form id="loginform"> Username:
    <input type = "text" name = "username" id ="username"> <br> Password:
    <input type = "password" name = "password" id = "password"> <br>
    <input type = "submit" name="submitbutton" id="submitbutton" class = "bluebutton"> 
    </form>
    <form><br><br><br>Don't have an account yet?<br><button id="registerbutton" name="registerbutton" href="register.html">Register Now</button>    

</Div>
<Div id="registerdiv">
    <br><br><h2>Sign Up For QuotesLib</h2>
    <br><form id="registerform"> Username:
    <input type ="text" id="regusername"> <br> Password:
    <input type ="password" id="reg1password"> <br> Repeat Password:
    <input type ="password" id="reg2password"> <br> First Name:
    <input type ="text" id="regfirstname"> <br> Last Name:
    <input type ="text" id="reglastname"> <br> email:
    <input type ="text" id="regEmail"> <br>
    <input type ="submit" id ="registersubmitbutton">
    <br> </form>
</Div> 

如果有人能给我这个问题的答案,那将是非常有帮助的!

4

2 回答 2

0
$("#registerdiv").append("<h3> class='loginerror'>Server error, retry</h3>");

应该

$("#registerdiv").append("<h3 class='loginerror'>Server error, retry</h3>");

看到区别: <h3>...<h3 ...

于 2012-08-18T02:52:03.457 回答
0

代码有一些错误,

第一的,

<form><br><br><br>Don't have an account yet?<br><button id="registerbutton" name="registerbutton" href="register.html">Register Now</button>

您有一个form打开标签但没有一个关闭标签,这个按钮甚至不需要表单标签,这是问题的主要原因

二、 JS代码结束,

}, "json");

}

应该

}, "json");
    });
}​

我不确定你是否在复制时错过了,

第三,

append 方法中的<h3>标签,尽管这与您遇到的问题无关。

最好使用一些文本编辑器来高亮打开代码、结束标签、大括号。

这里是工作代码

于 2012-08-18T05:06:50.257 回答