weixin_33721427 2015-05-15 15:53 采纳率: 0%
浏览 20

Ajax / JQuery 2次提交

I am trying to write a page where there are 2 sets of forms submitted, one to search IMDB, and then the 2nd to submit the request from the returned results.

I have the pages working just fine with just regular old php. I am wanting to convert it over to ajax/jquery to make it have a better ux, when there are large searches and database calls. I am having issues with getting the 2nd Submit button to work. I have searched for quite a while and noticed some comments about not using event.preventDefault() because any button called there after would not work and to use return false; instead. I have tried multiple combinations, even tried having it load a 2nd set of code during the posting.done of the 1st one, all to no avail.

I am trying to really understand what is going on here and what the proper solution would be.

All of this sits inside a div #dynamic

The script:

// Attach a submit handler to the form
$( "#imdb_search" ).submit(function( event ) {
    // Stop form from submitting normally
    //event.preventDefault();

    // Get some values from elements on the page:
    var $form = $( this ),
        form_request = $form.find( "input[name='request']" ).val(),
        form_user = $form.find( "input[name='user']" ).val(),
        form_username = $form.find( "input[name='username']" ).val(),
        url = $form.attr( "action" );

    // Send the data using post
    var posting = $.post(url, { 
        request: form_request, 
        user: form_user, 
        username: form_username
    });

    // Put the results in a div
    posting.done(function(data) {
        // var content = $( data ).find( "#returned_result" );
        $("#dynamic").empty().append(data);
    });
    return false;
});

So I have tried this as well from one of the comments to no avail:

$( "#imdb_search" ).submit(function(){
    var $frm = $(this);
    $.ajax({
       url: $frm.attr( "action" ),
       dataType: 'html',
       type: 'POST',
       data: $frm.serialize(),
       success: function(r) {
          $("#dynamic").empty().html(r);
       },
       error: function(){
          //TODO: add error code
       }
    });
return false;
});

I have this script at the bottom of each of the new php pages that is loaded from the success return. Is that the correct place to have it for DOM? Both forms have the same id but submit different "inputs".

  • 写回答

1条回答 默认 最新

  • weixin_33695082 2015-05-15 16:01
    关注

    As I can get from your code you also overwrite html form when you get first result, if it is the case then you should use jquery "on" function

    $(document).on("#imdb_search", "submit", function(){
    var $frm = $(this);
    $.ajax({
       url: 'your_post_url',
       dataType: 'html',
       type: 'POST',
       data: $frm.serialize(),
       success: function(r) {
          $("#dynamic").html(data);
       },
       error: function(){
          //TODO: add error code
       }
    });
    return false;
    });
    

    and be sure that you don't overwrite this code with other.

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改