How to make AjaxForm work with several forms loaded using jquery load()?

前端 未结 2 351
花落未央
花落未央 2021-01-22 18:44

I have a page with a div that is dynamically filled using a paginator ;-) At page init I load the first 10 forms in it using jquery .load() method.

What I\'d like to do

相关标签:
2条回答
  • 2021-01-22 19:00

    I'll try and address these one at a time to better match the question:

    1) You can re-bind when you .load() (or whatever jQuery ajax method you're using) or use a plugin like livequery(), for example here's re-binding (do this in your success handler):

    $("#myDynamicDiv .myForm").ajaxForm({ ...options... }); 
    

    Or using livequery():

    $(".myForm").livequery(function() { $(this).ajaxForm({ ...options... }); });
    

    2) Use a class instead of IDs here, like this: class="myForm", whenever you want to handle batches of elements like this class is a pretty safe route. The examples above work with class and not IDs per form (they can have IDs, they're just not used). Your form tags would look like this:

    <form class="myForm">
    

    3) The same solutions in answer #1 account for this :)

    0 讨论(0)
  • 2021-01-22 19:14

    ID values are unique to a single DOM element. So you'd need to give each form a new ID, so if you had three forms, you could name them like so:

    <form name="formone" id="formone"...
    <form name="formtwo" id="formtwo"...
    <form name="formthree" id="formthree"...
    

    Now you'd create instances of your ajax request like so:

         $('#formone, #formtwo, #formthree').ajaxForm({
           beforeSubmit: showLoader,
           success: hideLoader
         }); 
    
    0 讨论(0)
提交回复
热议问题