处理$(" form")。s​​ubmit或< input type =" submit">点击事件? [英] Difference between handling $("form").submit or an <input type="submit"> click event?

查看:131
本文介绍了处理$(" form")。s​​ubmit或< input type =" submit">点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下HTML:

 < form> 
...某些表单域...
< input type =submitid =submitButtonvalue =Submit/>
< / form>

我有一个javascript方法 validate 检查表单字段是否存在各种无效方案,如果一切正常,则返回 true ;如果有问题,则返回 false p>

在jQuery中,这样做有什么真正的区别:

  $( form)。submit(function(){
return validate();
});

...或者这样做:

<$ ();
return validate();
});

两者之间有什么优点/缺点吗?


Say I have the following HTML:

<form>
    ...some form fields...
    <input type="submit" id="submitButton" value="Submit" />
</form>

And I have a javascript method validate that checks the form fields for various invalid scenarios, returning true if all is well or false if there's something wrong.

Is there any real difference in jQuery between doing this:

$("form").submit(function() {
    return validate();
});

...or doing this:

$("#submitButton").click(function(){
    return validate();
});

And are there any advantages/disadvantages between the two?

解决方案

The click callback is called only if the user actually click on the submit button. But there are cases in which you would submit the form automatically by javascript, in that case the click callback is not fired while the submit callback is. I would recommend to use always the submit for validation and intrinsic action of the form, while using the click callback for animation or other things related to the action of clicking on the button.

这篇关于处理$(&quot; form&quot;)。s​​ubmit或&lt; input type =&quot; submit&quot;&gt;点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆