How to submit a specific form if multiple forms are present in one page using jquery

后端 未结 6 2069
-上瘾入骨i
-上瘾入骨i 2021-02-14 09:49

I have two forms.

相关标签:
6条回答
  • 2021-02-14 10:02

    you would say

    <input type="submit" name="btn1" id="btn1" value="Submit"/>

     $("#btn1").click(function(){  
      $("#frm1").submit();
    }
    

    and

    <input type="submit" name="btn2" id="btn2" value="Submit"/>

     $("#btn2").click(function(){  
      $("#frm1").submit();
    }
    
    0 讨论(0)
  • 2021-02-14 10:06

    I usually avoid the .submit() function as i almost always have to do something more with response that .submit() would allow me to do.

    Thus, a non .submit option where you SUBMIT buttons would have to be changed to normal buttons.

    $('.btn2').bind('click', function(){
    
        var form1Data = $('#frm1').serialize();
        $.ajax({
            url : 'someurl',
            type : 'post',
            datatype : 'json',
            data : form1Data,
            success : function(json) {
                sumbitForm2();
            }
        });
    });
    
    function submitForm2() {
    
        var form2Data = $('#frm2').serialize(); 
        $.ajax({
            url : 'urlToSumbitForm1',
            type : 'post',
            datatype : 'json',
            data : form2Data,
            success : function(json) {
                //do something if you need to
            }
        });
    }
    
    0 讨论(0)
  • 2021-02-14 10:10
    <button type="submit" form="form1" value="Submit">Submit</button>
    

    The form attribute specifies the id of the form that the button will submit.

    0 讨论(0)
  • 2021-02-14 10:14

    HTML

    <form name="frm1" action="someurl" method="post" id="frm1">
    <input type="submit" name="btn1" class="buttons" value="Submit"/>
    </form>
    
    
    <input type="submit" name="btn2" onclick="formSubmit()" value="Submit"/>
    

    Javascript

    <script>
    function formSubmit()
    {
    document.getElementById("frm1").submit();
    }
    </script>
    
    0 讨论(0)
  • 2021-02-14 10:17

    consider the HTML:

        <form id="target" action="destination.html">
        <input type="text" value="Hello there" />
        <input type="submit" value="Go" />
        </form>
        <div id="other">
        ....
        </div>
    

    The event handler can be bound to the form:

        $('#target').submit(function() {
        alert('Handler for .submit() called.');
        return false;
        });
    

    Click function:

        $('#other').click(function() {
        $('#target').submit();
        });
    

    Here is the link have a look: How can I submit form on button click when using preventDefault()?

    0 讨论(0)
  • 2021-02-14 10:23

    You can use ajax to submit the first form before the second one:

    $('form[name=frm2]').submit(function() {
        var form1 = $('form[name=frm1]');
        $.ajax({
            type: "POST",
            url: form1.attr('action'),
            data: form1.serialize(),
            async: false;
        });
    });
    
    0 讨论(0)
提交回复
热议问题