Form submit without refresh using jquery/ajax if page have more than one form

前端 未结 4 884
抹茶落季
抹茶落季 2020-12-11 10:14

Hi all I know that its very easy to submit a form without refreshing if there is only one form on the page but what about if there are more than one form on the page. Im usi

4条回答
  •  感动是毒
    2020-12-11 10:39

    just thought I'd add to this, if you have multiple forms on one page, you can set a function to act on all forms independently by getting the form's action value. As follows (tested with jQuery 1.8.3):

     $('form').submit(function(){
       var action = $(this).attr("action")
       $.ajax({
         url: action,
         type:'POST',
         data: $(this).serialize(),
         success: function(){
                //alert message
            }                   
         })
       return false
     })
    

    Hope this helps someone out!

提交回复
热议问题