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

后端 未结 6 1675
暗喜
暗喜 2020-12-30 02:56

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

$(\'#myForm\').submit(function()
{ ....
相关标签:
6条回答
  • 2020-12-30 03:20

    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.

    0 讨论(0)
  • 2020-12-30 03:26

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

    0 讨论(0)
  • 2020-12-30 03:32

    You could try -

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

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

    0 讨论(0)
  • 2020-12-30 03:32
    <input type="button" id="btn" value="click">
    

    jquery

         $(function(){
            $('#myForm').submit(function()
                 { ....});
    
             $("#btn").click(function(){
             $('#myForm').trigger('submit');
            });
         });
    
    0 讨论(0)
  • 2020-12-30 03:33

    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.

    0 讨论(0)
  • 2020-12-30 03:44
    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?)

    0 讨论(0)
提交回复
热议问题