.submit()没有在表单上触发

时间:2016-07-02 21:43:48

标签: jquery forms onsubmit

我有以下表格

<form action="" method="post" name="loginForm" id="loginForm">
    <div id="spinnerInsert">
        <div class="form-content"><input type="text" name="loginUser" id="loginUser" class="validate[required,minSize[3]]" required /><span class="form_highlight"></span><span class="form_bar"></span><label for="loginUser">Username</label><span class="form_hint">Enter your username here</span></div>
        <div class="form-content"><input type="password" name="loginPassword" id="loginPassword" class="validate[required]" required /><span class="form_highlight"></span><span class="form_bar"></span><label for="loginPassword">Password</label><span class="form_hint">Enter your password here</span></div>
    </div>
    <div class="form-content buttons"><p><input name="action-login" type="submit" id="action-login" value="Sign In" class="button confirm" style="width: 140px;" /><br/><a href="">Register</a> &middot; <a href="">Forgot Password</a></p></div>
</form>

然后脚本中的以下代码

$('form#loginForm').submit(function(event) {
    // Get data and submit
    $('#spinnerInsert').spin();
    $.getJSON("lib/class/class.login.php", {loginUser: $('input#loginUser').val(), loginPassword: $('input#loginPassword').val(), queryType: "1"})
    .done(function(response) {
        $('#spinnerInsert').spin(false);
        if(response.success===0){
            // Show Error
            toastr.error(response.message, 'Error');
            // If totalFails > 4, lock account and offer to reset password or email admin
            if(response.totalFails>4) {
                $('article#pageForm').html('<p id="accountLocked">Sorry, your account has been locked. Please <a href="">reset your password</a> or <a href="mailto:adminemail">contact the site admin</a> to unlock your account</p><p><a href="javascript:backToLogin();">Back to Sign In</a></p>');
            }
        } else {
            // Load Authentication Section
            $('article#pageForm').html(response.data);
        }
    });
    event.preventDefault();
    return false;
});

但由于某些原因,每次按提交时它都会重新加载页面。它根本不会触发我的脚本。

昨天工作,但不是今天,我不记得改变了什么。

我也试过$(document).on('submit','form#loginForm',function(event) {

http://spendit.dpdesignz.co/

的完整演示

1 个答案:

答案 0 :(得分:1)

问题在于.spin()未定义。尝试删除包含.spin()的行并尝试提交表单。

$('form#loginForm').submit(function(event) {
    // Get data and submit
    $.getJSON("lib/class/class.login.php", {loginUser: $('input#loginUser').val(), loginPassword: $('input#loginPassword').val(), queryType: "1"})
    .done(function(response) {
        if(response.success===0){
            // Show Error
            toastr.error(response.message, 'Error');
            // If totalFails > 4, lock account and offer to reset password or email admin
            if(response.totalFails>4) {
                $('article#pageForm').html('<p id="accountLocked">Sorry, your account has been locked. Please <a href="">reset your password</a> or <a href="mailto:adminemail">contact the site admin</a> to unlock your account</p><p><a href="javascript:backToLogin();">Back to Sign In</a></p>');
            }
        } else {
            // Load Authentication Section
            $('article#pageForm').html(response.data);
        }
    });
    event.preventDefault();
    return false;
});