Can't submit generated form

拥有回忆 提交于 2020-03-03 07:44:18

问题


I want to submit a form that's been created dynamically (by a previous form submit) using AJAX, but I'm not able to get it to work successfully. The generated form has the format shown, and is being created successfully

<div id="1" class="container-element">
    <h3>HEADER</h3>
    <form id="formCELL" name="form_1" action="refresh.php" method="post">
        <div class="panel-body">
            <div class="col-xs-10 com">CONTENT GOES HERE</div>                                                                                                                               
            <div class="col-xs-2 btn"><button class="btn btn-primary" id="refresh" type="submit">Refresh</button>                                                                </div>                                                            
        </div>                                                        
    </form>                                                      
</div>

The functions used to generate the form, and then refresh the content, is here. Right now, the $('#formCELL').submit(function(event) function isn't being entered when the refresh button is clicked.

$(document).ready(function() {

    $('#formMAIN').submit(function(event) {

        //formCELL is generated here through AJAX call.....

    });

    $('#formCELL').submit(function(event) {

        // process the form
        alert("about to submit");
        //AJAX call to go here

        event.preventDefault();
    });

});

I'm guessing that the problem has to do with me having my submit function within $(document).ready, that the form I want to refresh hasn't been created at this point yet, but I'm not sure how to continue from here.


回答1:


Try:

$(document).on('submit', '#formCELL', function(event) { ... });

I create a jsFiddle for you: JsFiddle

I modify the jsFiddle according your needs. Now first form generate second form that is handled by seconf submit function




回答2:


You are right. Move the second submit handler into the first one, after the creation of the form, like this:

$(document).ready(function() {

  $('#formMAIN').submit(function(event) {

    $.ajax(....).done(function(result) {
      //formCELL is generated here ...
      $('#formCELL').submit(function(event) {

        // process the form
        alert("about to submit");
         //AJAX call to go here

         event.preventDefault();
      });
    } 

  });

});



回答3:


$(document).ready(function() {

$('#formCELL').submit();

$('#formCELL').on('submit', function(event) {

    // process the form
    alert("about to submit");
    //AJAX call to go here

    event.preventDefault();
});

});




回答4:


Try this

$(document).delegate('#formCELL','submit',function(event) {

        // process the form
        alert("about to submit");
        //AJAX call to go here

        event.preventDefault();
    });


来源:https://stackoverflow.com/questions/39465212/cant-submit-generated-form

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