Jquery - 如何触发 $('#myForm').submit(function()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7317223/
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-08-26 23:41:29  来源:igfitidea点击:

Jquery - How to trigger $('#myForm').submit(function()

jquerytriggerssubmit

提问by Peter

how can i trigger this function without using the form submit?

如何在不使用表单提交的情况下触发此功能?

$('#myForm').submit(function()
{ ....

回答by ipr101

You could try -

你可以试试——

$('#myForm').trigger('submit');

Working demo - http://jsfiddle.net/ipr101/KvwMb/1/

工作演示 - http://jsfiddle.net/ipr101/KvwMb/1/

回答by Vins

You can directly use something like this

你可以直接使用这样的东西

$('#button1').click(function(){
    $('#search').submit();
});

Or you can use it from any javascript code. Which will trigger your $('#myForm').submit(function(){.....function code.

或者您可以从任何 javascript 代码中使用它。这将触发您的$('#myForm').submit(function(){.....功能代码。

回答by NimChimpsky

return false;

adding this to function will stop the form submitting.

将此添加到函数将停止提交表单。

Or if you want to call function on a different event put the function in an appropriate event handler (a non-submit button's click?)

或者,如果您想在不同的事件上调用函数,请将函数放在适当的事件处理程序中(非提交按钮的点击?)

回答by simplyharsh

You will have to dig it from DOM data. But, let me tell you, its not recommended. Try this,

您将不得不从 DOM 数据中挖掘它。但是,让我告诉你,它不推荐。尝试这个,

var form = $('#myForm');
var data = form.data();
var events = data.events;

If the handler function is attached to form 'submit', it will be present as,

如果处理程序函数附加到表单“提交”,它将显示为,

var submit_list = events.submit;

Now, if all goes well, at best you can get submit_listas list of all the handler objects attached to submit event of form.

现在,如果一切顺利,您最多可以获得submit_list附加到表单提交事件的所有处理程序对象的列表。

Shortcut:Assuming you have one and only one handler attached to submit event for #myForm,

快捷方式:假设您有一个且只有一个处理程序附加到提交事件#myForm

$('#myForm').data().events.submit[0].handler

is your function.

是你的功能。

Play with data(), but remember its not recommended. Happy Coding.

data(),但记住它不推荐。快乐编码。

回答by Rafay

<input type="button" id="btn" value="click">

jquery

查询

     $(function(){
        $('#myForm').submit(function()
             { ....});

         $("#btn").click(function(){
         $('#myForm').trigger('submit');
        });
     });

回答by Dmitry Maksimov

Extract code from callback to function and call it when it is needed.

从回调函数中提取代码并在需要时调用它。