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

送分小仙女□ 提交于 2020-01-10 09:51:48

问题


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

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

回答1:


You could try -

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

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




回答2:


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.




回答3:


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?)




回答4:


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

jquery

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

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



回答5:


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

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_list as list of all the handler objects attached to submit event of form.

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

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

is your function.

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




回答6:


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



来源:https://stackoverflow.com/questions/7317223/jquery-how-to-trigger-myform-submitfunction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!