在更改事件上停止表单操作

时间:2016-01-04 14:16:56

标签: javascript jquery validation

当我在更改事件时出现错误时,有没有办法停止表单操作..

$(".accuplod").change(function(){
    $(".errorjp").remove();
    var files = $(this)[0].files;
    var arr = [ 'png', "jpeg", "jpg" ];

    for (var i = 0; i < files.length; i++) {
        var extarr = files[i].name.split(".");
        extntn = extarr[1].toLowerCase();

        if (files[i].size > 250000) {
            $(".accclass").after("<div class='errorjp'><label class='form-error'>There was an upload error.Make sure the file " + files[i].name + " having given size.</label></div>");
            // here I want to stop the form action   
        }

        if ($.inArray(extntn, arr) == -1) {
            $(".accclass").after("<div class='errorjp'><label class='form-error'>There was an upload error.Make sure the file " + files[i].name + " is JPG, JPEG, or PNG.</label></div>");
            // here I want to stop the form action 
        }
    }
});

如果上面的文件上传功能有任何错误,如何停止表单操作?

1 个答案:

答案 0 :(得分:0)

您可以将提交处理程序附加到执行类似以下内容的表单...

$(formSelector).on('submit', function(e){
    if ($(".accclass").find(".errorjp").length) {
        e.preventDefault();
        console.log('Submit was cancelled.  An error exists.');
    }
});

这是假设他们更改输入,获取错误,然后提交表单,因为我没有看到您的逻辑正在提交。