如何在validate plugin submitHandler中返回false工作

时间:2015-12-02 21:39:32

标签: javascript jquery ajax

我刚刚浏览了验证器plgin示例,并在网上找到了一段代码:

$('#testimonials-form').validate({
    rules: {
        "t-title": {
            required: true,
            minlength: 5
        },
        "t-text": {
            required: true,
            minlength: 15
        },
        "person-name": {
            required: true,
            minlength: 4
        },
        "photo": {
            required: true,
            accept: 'image/*'
        }
    },
    messages: {
        "t-title": {
            required: "A title is needed",
            minlength: "minimum 4 characters"
        },
        "t-text": {
            required: "A testimonial is needed",
            minlength: "minimum 15 characters"
        },
        "person-name": {
            required: "A name is needed",
            minlength: "minimum 4 characters"
        },
        "photo": {
            required: "An image for the testimonial giver is needed",
            accept: "Only image file type is accpeted , please check that the file you tried to upload was an image"
        }
    },

    submitHandler: function (form) {

        var formData = new FormData();
        formData.append('photo', $('input[type=file]')[0].files[0]);
        formData.append('t-title', $('input[name=t-title]').val());
        formData.append('t-text', $('textarea[name=t-text]').val());
        formData.append('person-name', $('input[name=person-name]').val());


        $.ajax({
            type: 'POST', // define the type of HTTP verb we want to use (POST for our form)
            url: 'inc/database.php', // the url where we want to POST
            data: formData,
            // THIS MUST BE DONE FOR FILE UPLOADING
            contentType: false,
            processData: false,
            // ... Other options like success and etc
        });

        return false;

    }
});

现在return false如何在这里工作,尽管没有附加到提交按钮?我使用上面的代码,它工作得非常好,我只是想知道上面的代码是如何工作的,特别是return false是如何工作的?

编辑 ::我知道return false是如何运作的,我问他们如果没有附加到提交按钮,它是如何工作的。

谢谢。

1 个答案:

答案 0 :(得分:-1)

当注册表单上的提交事件时,将调用提交处理程序。它不必与按钮绑在一起,只是事件。