0

嗨,我正在尝试使用 ajax 将简单的用户名和密码发送到 login.php,这将返回 2 结果为假或真。问题是,当我单击#loginbutton 时,它似乎正在加载,但之后什么都没有出现(没有警报或页面重新加载)。

这是我的脚本

    <script>
$(document).ready(function(){
    $('#loginbutton').click(function() {
$('#loginform').submit(function() { // catch the form's submit event
    $.ajax({ // create an AJAX call...
        data: $(this).serialize(), // get the form data
        type: $(this).attr('method'), // GET or POST
        url: $(this).attr('action'), // the file to call
        success: function(result) { // on success..
            if (result == 'false') {
            alert("Login failed.\n\nThe username and password doesn't match or perhaps the username doesn't exist.\n\nMake sure you have checked your email and validate your account.");
            }
            else if (result == 'true') {
            alert("Thank you, the registration was successful. \nWe have sent you an email, please validate your account. \nClick OK and we will redirect you to the homepage.");
            window.location.replace("http://127.0.0.1/wordpress/");
        }
        }
    });
    return false; // cancel original event to prevent form submitting
});     
});
});
</script>

和我的登录表单(我正在尝试将其与 wordpress 集成)

<?php 
                        $templateDirectory= get_bloginfo('template_directory');

                            echo'
                        <form id="loginform" action="'.$templateDirectory.'/login.php" method="post" class="login">
                        <a href="#" onClick="document.getElementById(\'loginform\').submit();" class="linkit">LOGIN</a>
                        <div class="para"><input type="text" name="uname" placeholder="Username ..." onkeydown="if (event.keyCode == 13) document.getElementById(\'loginform\').submit()"> <br><input type="password" name="pass" placeholder="Password ..." onkeydown="if (event.keyCode == 13) document.getElementById(\'loginform\').submit()"></div>
                        </form>';

?>

任何人都可以告诉我出了什么问题:(

4

1 回答 1

1

您正在单击处理程序中添加表单提交处理程序...这不是必需的...尝试

$(document).ready(function () {
    $('#loginform').submit(function () { // catch the form's submit event
        $.ajax({ // create an AJAX call...
            data: $(this).serialize(), // get the form data
            type: $(this).attr('method'), // GET or POST
            url: $(this).attr('action'), // the file to call
            success: function (result) { // on success..
                if (result == 'false') {
                    alert("Login failed.\n\nThe username and password doesn't match or perhaps the username doesn't exist.\n\nMake sure you have checked your email and validate your account.");
                } else if (result == 'true') {
                    alert("Thank you, the registration was successful. \nWe have sent you an email, please validate your account. \nClick OK and we will redirect you to the homepage.");
                    window.location.replace("http://127.0.0.1/wordpress/");
                }
            }
        });
        return false; // cancel original event to prevent form submitting
    });
});
于 2013-10-26T15:45:01.413 回答