我无法提交自助表格

时间:2014-10-09 21:16:02

标签: javascript jquery forms twitter-bootstrap

我有这个表格在我的网站上注册我的用户:

<div class="pages_container">
    <form id="registerForm" method="post" class="form-horizontal">
        <div class="form-group">
            <label class="col-lg-3 control-label">Full name</label>
            <div class="col-lg-3">
                <input type="text" class="form-control" name="firstName" placeholder="First name" />
            </div>
            <div class="col-lg-3">
                <input type="text" class="form-control" name="lastName" placeholder="Last name" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">Username</label>
            <div class="col-lg-6">
                <input type="text" class="form-control" name="username" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">Password</label>
            <div class="col-lg-6">
                <input type="password" class="form-control" name="password" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">Retype password</label>
            <div class="col-lg-6">
                <input type="password" class="form-control" name="confirmPassword" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">Email address</label>
            <div class="col-lg-6">
                <input class="form-control" name="email" type="email" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-3 control-label">Phone number</label>
            <div class="col-lg-6">
                <input type="text" class="form-control" name="phone" />
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-6 col-lg-offset-3">
                <button type="submit" class="btn btn-primary btn-lg btn-block">Register</button>
            </div>
        </div>      
        </div>
    </form>
</div>

我使用这个JQuery来验证表单,然后将其提交到PHP页面以存储在MySQL数据库中:

$(document).ready(function(){
    //To validate the registration form and save its value after validation
    $('#registerForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            firstName: {
                validators: {
                    notEmpty: {
                        message: 'The first name is required'
                    },
                    stringLength: {
                        min: 2,
                        message: 'The first name must be at least 2 characters long'
                    },
                    regexp: {
                        regexp: /^[a-z\s]+$/i,
                        message: 'The first name can consist of alphabetical characters and spaces only'
                    }
                }
            },
            lastName: {
                validators: {
                    notEmpty: {
                        message: 'The last name is required'
                    },
                    stringLength: {
                        min: 2,
                        message: 'The last name must be at least 2 characters long'
                    },
                    regexp: {
                        regexp: /^[a-z\s]+$/i,
                        message: 'The last name can consist of alphabetical characters and spaces only'
                    }
                }
            },
            username: {
                message: 'The username is not valid',
                validators: {
                    notEmpty: {
                        message: 'The username is required and cannot be empty'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: 'The username must be more than 6 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9]+$/,
                        message: 'The username can only consist of alphabetical and number'
                    },
                    different: {
                        field: 'password',
                        message: 'The username and password cannot be the same as each other'
                    },
                    remote: {
                        message: 'The username is not available',
                        url: 'include-ajax/username_or_email_availablitiy.php',
                        data: {
                            type: 'username'
                        }
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: 'The password is required and cannot be empty'
                    },
                    different: {
                        field: 'username',
                        message: 'The password cannot be the same as username'
                    },
                    stringLength: {
                        min: 8,
                        message: 'The password must have at least 8 characters'
                    },
                    identical: {
                        field: 'confirmPassword',
                        message: 'The password and its confirm are not the same'
                    }
                }
            },
            confirmPassword: {
                        validators: {
                    notEmpty: {
                        message: 'The password is required and cannot be empty'
                    },
                    different: {
                        field: 'username',
                        message: 'The password cannot be the same as username'
                    },
                    stringLength: {
                        min: 8,
                        message: 'The password must have at least 8 characters'
                    },
                    identical: {
                        field: 'password',
                        message: 'The password and its confirm are not the same'
                    }
                }
            },
            email: {
                validators: {
                    notEmpty: {
                            message: 'The email is required and cannot be empty'
                    },
                    emailAddress: {
                        message: 'The input is not a valid email address'
                    },
                    remote: {
                        message: 'The email is not available',
                        url: 'include-ajax/username_or_email_availablitiy.php',
                        data: {
                            type: 'email'
                        }
                    }
                }
            },
            phone: {
                validators: {
                    notEmpty: {
                        message: 'The phone number is required'
                    },
                    digits: {
                        message: 'The phone number can contain digits only'
                    },
                    stringLength: {
                        min: 11,
                        max: 11,
                        message: 'The phone number must be 11 digits'
                    }
                }
            },
            submitHandler: function(form) { // <- only fires when form is valid
                $.ajax({
                    type: 'POST',
                    url: 'include-ajax/check_and_save_registration_form.php',
                    data: $(form).serialize(),
                    success: function() {alert('test');
                        $(form).fadeOut(500, function(){
                            $(form).html("USER DONE!").fadeIn();
                        });
                    }
                });            // <- end '.ajax()'
                return false;  // <- block default form action
            }
        }
    });
});

验证过程运行正常,但问题是在点击提交按钮后,我在控制台Uncaught TypeError: Cannot read property 'attr' of null中出现javascript错误,此错误指向bootstrapValidator.min.js:12

说实话,这是我的第一个bootstrap代码,所以我不太了解submitHandler。 什么是arg form如果我们通过this而我们得到了什么? 脚本是否看到表单时出现错误的原因是什么?

0 个答案:

没有答案