douzuita7325 2014-04-17 10:27
浏览 60
已采纳

使用php发送POST请求后,jquery脚本无法正常工作

I have a page generated using php, and after i send a post request from page on itself javascript stop working. How can i fix it? I need to change my page content using php. My script looks like:

$(function(){
 my code here...
});

And i'm sending request with this function:

function sendPostRequest(params){
       var xmlhttp=new XMLHttpRequest();
       xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200)document.body.innerHTML=xmlhttp.responseText;};
       xmlhttp.open("POST","catalog_main.php",false);
       xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
       document.body.innerHTML="";
       xmlhttp.send(params);
     }
  • 写回答

2条回答 默认 最新

  • dougu1896 2014-04-17 10:33
    关注

    I'm guessing that when you say "javascript stops working" you mean that events which have a javscript function tied to them are not firing that function.

    I think the root of the problem is:

    document.body.innerHTML=xmlhttp.responseText
    

    If you bind a function to an event at page load then you are binding it to the event on a specific element. When you run the above you remove all elements in the page body and replace them with new ones. The new ones may have the same class/id but they are new elements which do not have any events bound.

    There are two solutions: either encapsulate all your event binding in a function, which you run both on page load and after replaceing the page as above or use only delegated events where all events are techically bound to the body element itself.

    Edit: I see the questions is tagged jQuery (I wonder why ypou are not using $.ajax in this case?). Using jQuery you can do the delegated events as such: Instead of

    $(function(){
       $('#foo').click(function(){
          alert('Foo clicked');
       });
       $('.bar').change(function(){
          //do something
       });
    });
    

    You can use

    $(function(){
       $('body')
       .on('click', '#foo', function(){
          alert('Foo clicked');
       })
       .on('change', '.bar', function(){
          //do something
       });
    });
    

    For more info see the jQuery docs on .on()

    Second Edit

    To use the first option (9encapsulate all events in a function) then, based on the same events as the above example, change the document ready to the following:

    function set_events(){
       $('#foo').click(function(){
          alert('Foo clicked');
       });
       $('.bar').change(function(){
          //do something
       });
    }
    $(function(){
       set_events();
    });
    

    Then change you ajax response habndler to xmlhttp.onreadystatechange = function(){

      if (xmlhttp.readyState==4 && xmlhttp.status==200){
         document.body.innerHTML=xmlhttp.responseText;
         set_events();
      }
    

    };

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 优质github账号直接兑换rmb,感兴趣伙伴可以私信
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)