jQuery方法:.submit()和.trigger('submit')之间的区别 [英] jQuery methods: difference between .submit() vs .trigger('submit')
问题描述
jQuery允许通过以下任一方式以编程方式触发表单提交:
jQuery allows to trigger a form submission programmatically via either:
-
$('.js-form-class-hook').submit();
$('.js-form-class-hook').trigger('submit');
注意:我的理解是.trigger('submit')
是.submit()
的意思,.on('submit',function(e){});
是.submit(function(e){});
的意思.简而言之,.trigger('submit')
是一种比.submit()
功能强大的编程提交表单的方法.
Note: My understanding is that .trigger('submit')
is to .submit()
what .on('submit',function(e){});
is to .submit(function(e){});
. In short .trigger('submit')
is a more powerfull way than .submit()
to programmatically submit forms .
我已经知道.on('submit',function(e){});
和.submit(function(e){});
之间的一些区别,请参见,我会现在想更好地了解.trigger('submit')
的作用.
I already know some of the difference between .on('submit',function(e){});
and .submit(function(e){});
, see my answer on What's the difference between $(form).submit and $(form).on("submit") in jQuery? , I would like to now understand better the role of .trigger('submit')
.
我的结论:经过研究,我发现使用.trigger('submit')
似乎提供了允许传递任意数据
My conclusion: after some research I found out that using .trigger('submit')
seems to provide the "only" (although very powerful) advantage of allowing to pass arbitrary data
示例用法1:
例如,这可以用于区分"人"与"程序化"表单提交.
This can for instance be used to differenciate a "human" vs "programmatical" form submission.
在 jsbin.com/jivexawixonu/1/edit?html,js,console,output
HTML
<form class="js-form-hook-xyz">
<button>Submit form</button>
</form>
jQuery
var pleaseConfirmSubmission = function( formElem ){
// ... some conditions checks
// if condition met, submit with a flag
formElem.trigger( "submit" , ['isProgrammaticalSubmission'] );
}
$("body").on("submit", ".js-form-hook-xyz", function(event, isProgrammaticalSubmission) {
console.log("on form submission: " + ( isProgrammaticalSubmission || "isHumanAction !" ) );
if ( !isProgrammaticalSubmission ){ /* JS truthy/falsy value usage */
event.preventDefault(); /* cancel form submission */
pleaseConfirmSubmission( $(this) );
}
});
资源:
- api.jquery.com/submit
- api.jquery.com/trigger
- api.jquery.com/on
- www.inkling .com/read/jquery-cookbook-cody-lindley-1st/chapter-8/recipe-8-5
- api.jquery.com/submit
- api.jquery.com/trigger
- api.jquery.com/on
- www.inkling.com/read/jquery-cookbook-cody-lindley-1st/chapter-8/recipe-8-5
我错过了.trigger('submit')
提供的其他功能吗?
Is there any other additional feature provided by .trigger('submit')
that I missed ?
或者允许传递任意数据"是使用.trigger('submit')
的唯一优势吗?
Or is "allowing to pass arbitrary data" the only advantage of using .trigger('submit')
?
推荐答案
不带参数的submit()
与trigger('submit')
基本上没有区别,实际上不带参数的submit()
最终仍将返回trigger()
There is essentially no different between submit()
without an argument, and trigger('submit')
, in fact submit()
with no arguments will eventually return trigger()
anyway.
您可以通过查看 jQuery来证明这一点来源:
jQuery.fn.submit()
function (data, fn) {
return arguments.length > 0 ? this.on(name, null, data, fn) : this.trigger(name);
}
因此,如果将参数传递给submit()
,则将调用.on('submit'...
,否则,将调用.trigger('submit')
.
So, if an argument is passed to submit()
, .on('submit'...
will be called, otherwise, .trigger('submit')
will be called.
submit()
几乎是一种更易理解的调用trigger('submit')
的方式.两者都没有特殊功能,您选择的是个人喜好.
submit()
is pretty much a more human-readable way of calling trigger('submit')
. There are no special features for either, which one you choose is personal preference.
允许传递任意数据"是使用.trigger('submit')的唯一优势吗?
is "allowing to pass arbitrary data" the only advantage of using .trigger('submit') ?
除非您认为少用一个函数调用是一个优势,否则确实如此.
Unless you consider one less function call an advantage, yes, it is.
这篇关于jQuery方法:.submit()和.trigger('submit')之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!