简体   繁体   English

可以单击Jquery Validation未经验证的表单提交按钮(使用Fiddle)

[英]Jquery Validation Unvalidated form submit button able to be clicked (With Fiddle)

I have a Jquery form with a validation script on the URL, it correctly validates and invalidates the URL. 我有一个带有URL上的验证脚本的Jquery表单,它可以正确地验证和使URL无效。 However, when it invalidates the URL, it still allows the form button to be clicked, but not submitted. 但是,当它使URL无效时,它仍然允许单击表单按钮,但不能提交。

This causes the hidden loading image to show even though the form is no being submitted. 即使没有提交表单,这也会显示隐藏的加载图像。

Here is a fiddle with the loader and script http://jsfiddle.net/mikeef74/gzSDH/16/ 这是一个关于加载程序和脚本的小提琴, 网址为http://jsfiddle.net/mikeef74/gzSDH/16/

var submit_hit = false;

$(document).ready(function () {
    $('#iframe1').on('load', function () {
        $('#loader1').hide();
        if (submit_hit) {
            $('#form_container').hide();
            $('#mydivhide1, #mydivhide2').show();
            $('body').css("background-image", "url(images/previewbg7.jpg)");
            $('body').css("overflow-y", "auto");
        }
    }
});

$("#form_710370").validate();
$('#form_710370').submit(function (e) {
    $('#loader1').show();
        submit_hit = true;
        return true;
    });
});

The submit event will trigger whenever you attempt to submit. 每当您尝试提交时,都会触发Submit事件。 You've defined a submit event handler tied to the form which is why the image is showing. 您已经定义了与表单绑定的Submit事件处理程序,这就是显示图像的原因。

Seems you want to trigger the loader image on a conditional submit so you'll need to use the validation's submitHandler . 似乎您想在条件提交上触发加载程序映像,因此您需要使用验证的submitHandler Now the inner function will only trigger when the form is valid -- the loader image will display and the form will be explicitly submitted. 现在,内部函数将仅在表单有效时触发-加载程序图像将显示,并且表单将被明确提交。

$("#form_710370").validate({
    submitHandler: function(form) {
        $("#loader1").show();
        submit_hit = true;
        form.submit();
    }
});

// remove this
//$('#form_710370').submit(function (e) { ... }

http://jqueryvalidation.org/documentation/ http://jqueryvalidation.org/documentation/

This is way validate a form in jquery: 这是在jquery中验证表单的方法:

 $(function() {
            $("#form_710370").validate({
                rules: {
                          //
                       },
                messages: {
                       //
                       },
        submitHandler: function(form) {
                    form.submit();
                }
            });
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM