jQuery提交处理程序不提交表单

时间:2017-02-14 09:21:38

标签: javascript jquery html forms

我有一个表格,需要在验证和确认请求后提交。

但是当我在弹出的确认提示中点击“确定”时,它没有做任何事情。

以下是代码:

$("#form").validate({

    onkeyup: false,
    ignore: 'input:hidden',
    async: false,
    rules: {
        username: {
            required: true,
        },
    },
    messages: {
        username: {
            required: "Please enter a username",
        },
    },
    submitHandler: function validateUser(){

        if(confirm('Are you sure??')){
        return true;
        }else {
        return false;
        }
        },
    errorElement: "span",
    wrapper: "span",
    errorPlacement: function (error, element) {
        error.appendTo(element.parent());
        error.css({
            color: 'red',
            display: 'block'
        });
    },
    highlight: function (element, errorClass) {
    }

});

2 个答案:

答案 0 :(得分:0)

试试这个简化版

$("#form").validate({    
    onkeyup: false,
    ignore: 'input:hidden',
    async: false,
    rules: {
        username: {
            required: true,
        },
    },
    messages: {
        username: {
            required: "Please enter a username",
        },
    },
    submitHandler: function(){
        return confirm('Are you sure??');
        },
    errorElement: "span",
    wrapper: "span",
    errorPlacement: function (error, element) {
        error.appendTo(element.parent());
        error.css({
            color: 'red',
            display: 'block'
        });
    },
    highlight: function (element, errorClass) {
    } 
});

答案 1 :(得分:0)

试试这个:

submitHandler: function(form){

    if(confirm('Are you sure??')){
       form.submit(); // or $(form).ajaxSubmit(); if you use ajax
    }else {
       return false;
    }
},