javascript 防止默认后重新启用提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9856942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:03:47  来源:igfitidea点击:

Re-enabling Submit after prevent default

javascriptjquery

提问by Michael Hahn

I am trying to unbind or reenable the prevent default so my form will submit on good data.

我正在尝试解除绑定或重新启用阻止默认值,以便我的表单将提交正确的数据。

I have tried multiple examples. Here is my code and some of the examples i tried.

我已经尝试了多个例子。这是我的代码和我尝试过的一些示例。

This code works great for what i want to. Just the last thing and resetting the div which i can implement after i get this.

这段代码非常适合我想要的。只是最后一件事并重置我可以在我得到这个后实现的div。

function lengthRestriction(elem, min, max) {
    var uInput = elem.value;
    if (uInput.length >= min && uInput.length <= max) {
        return true;
    } else {
        var cnt = document.getElementById('field');
        cnt.innerHTML = "Please enter between " + min + " and " + max + " characters";
        elem.focus();
        $('#ShoutTweet').submit(function(e) {
            e.preventDefault();

            //bind('#ShoutTweet').submit();
            //$('#ShoutTweet').trigger('submit'); 
        });
    }
}?

i have a jsbin set up too http://jsbin.com/ebedab/93

我也设置了一个 jsbin http://jsbin.com/ebedab/93

采纳答案by Chris Laplante

I'm not completely sure what you are asking, but if your goal is to destroy your custom submithandler, then use this:

我不完全确定你在问什么,但如果你的目标是销毁你的自定义submit处理程序,那么使用这个:

$("#ShoutTweet").unbind("submit");

This assumes that you have a normal (not Ajax) form.

这假设您有一个正常的(不是 Ajax)表单。

回答by nnnnnn

Don't try to set up and cancel a submit handler from within your validation function, do it the other way around: call the validation from within a single submit handler, and only call .preventDefault()if the validation fails:

不要尝试从验证函数中设置和取消提交处理程序,相反:从单个提交处理程序中调用.preventDefault()验证,并且仅在验证失败时调用:

$(document).ready(function() {

    $('#ShoutTweet').submit(function(e) {
       if (/* do validations here, and if any of them fail... */) {
          e.preventDefault();
       }
    });

});

If all of your validations pass just don't call e.preventDefault()and the submit event will then happen by default.

如果您的所有验证都通过了就不要调用e.preventDefault(),那么默认情况下将发生提交事件。

Alternatively you can return falsefrom your submit handler to prevent the default:

或者,您可以false从提交处理程序返回以防止默认:

    $('#ShoutTweet').submit(function(e) {
       if (!someValidation())
          return false;

       if (!secondValidation())
          return false;

       if (someTestVariable != "somevalue")
          return false;

       // etc.
    });

回答by max

Just call submit on the form

只需在表单上调用提交

$('#ShoutTweet').submit();

回答by Ankit Bansal

This works surely and enable form submission after event.preventDefault();

这肯定有效,并在之后启用表单提交 event.preventDefault();

$('#your-login-form-id').on('submit', onSubmitLoader);

function onSubmitLoader(e) {
    e.preventDefault();
    var self = $(this);
    setTimeout(function () {
        self.unbind('submit').submit(); // like if wants to enable form after 1s
    }, 1000)
}