jQuery如果myform.valid()为true则触发另一个jQuery函数?

前端之家收集整理的这篇文章主要介绍了jQuery如果myform.valid()为true则触发另一个jQuery函数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个jQuery函数,我想只在表单验证为真时才执行
我有验证和其他jQuery工作正常.
问题是现在验证和其他功能都在同一时间执行.

我想仅在表单验证为真时才运行其他函数.
这是我的剧本

required: true
                    }
                });
                $("#myform").validate();
                if ($("#myform").valid()) {
                    $(function () {
                        $('#myform').submit(function () {
                            var cBoxes = ($('input:checkBox:checked').filter(":checked").length);
                            var nBoxes = ($(":checkBox:not(:checked)").length);
                            if ((cBoxes > 0) && (nBoxes > 0)) {
                                confirm('You have Checked only few locations among the List. \n Are you sure,you do not want to Prescribe from the other locations? ');
                            } else if (cBoxes == 0) {
                                alert('Please select atleast One Address \n Where you would prefer to prescribe from.');
                                return false;
                            }
                            else {
                                return true;
                            }
                        });
                    });
                }
            });

请告诉我怎样才能完成这项工作?

最佳答案
如果您使用的是standard plugin,则应支持此类代码

$(document).ready(function() {
    $("#myForm").validate({ 
        submitHandler: function(form) {
            SomeFuncHere();
        }
    });
})

成功验证后,应调用submitHandler中的代码.

编辑:根据您的代码,尝试这样做:

$("#myform").validate({ 
    submitHandler: function(form) {
        var cBoxes = ($('input:checkBox:checked').filter(":checked").length);
        var nBoxes = ($(":checkBox:not(:checked)").length);
        var flag = false;
        if ((cBoxes > 0) && (nBoxes > 0)) {
            flag = confirm('You have Checked only few locations among the List. \n Are you sure,you do not want to Prescribe from the other locations? ');
        } else if (cBoxes == 0) {
            alert('Please select atleast One Address \n Where you would prefer to prescribe from.');
        }
        else {
            flag = true;
        }
        return flag;
    }
});
原文链接:https://www.f2er.com/jquery/428559.html

猜你在找的jQuery相关文章