duanbicheng3345 2012-09-07 20:47
浏览 24
已采纳

ajax电话只能第一次使用

I have a method in my controller that lets users check something called "POE Status". This returns a string that i display in a div tag on my view. I also provide 2 buttons. one to enable POE and the other to disable. Both the enable and disable commands will return the POE status as a result. So I'm trying to use ajax to update the page with the new status once either one of the two buttons are clicked.

What I'm noticing is that both buttons will work the first time the page is loaded. But after the initial ajax call is made and the view updated, if i try to use the buttons again, nothing happens.

I have the following HTML: (i'm not including all the html, just the parts that I think matter....)

  <div class="span6" id="clientajaxcontainer">

      <h2>Port POE Status: Port <?php echo $portname;?></h2>
      <p><p>
      <?php echo $poeStatus;?>                
      <button class="btn" id="enable">Enable POE</button>&nbsp;
      <button class="btn" id="disable">Disable POE</button>              
  </div>

Then I have the following javascript:

  <script>
 //Enable POE button
  $('#enable').click(function()  {
   alert('in POEOn');
   console.log('On');
  //disbled button until ajax request is done. 
  $(this).attr("disabled","disabled");
  $('#disable').attr("disabled","disabled");
  var htmlstring;
  $.ajax({
    url:"<?php echo site_url('controller/poeOn);?>",
    type:'POST',
    dataType:'text',
    success: function(returnDataFromController) {
    htmlstring = "<h2>Port POE Status: Port " + $('#port').val() + "</h2><p><p>" + returnDataFromController + "<button class='btn' name='enable' id='enable'>Enable POE</button>&nbsp;<button class='btn' name='disable' id='disable'>Disable POE</button>";    
    alert(htmlstring);
    $('#clientajaxcontainer').html(htmlstring); 
    console.log(htmlstring);
  }
});
//re-enable the buttons.
$(this).removeAttr("disabled");
$('#disable').removeAttr("disabled");
  });


//Disable POE button
  $('#disable').click(function()  {
alert('in POEoff');
console.log('Off');
//disbled button until ajax request is done. 
$(this).attr("disabled","disabled");
$('#enable').attr("disabled","disabled");

$.ajax({
  url:"<?php echo site_url('controller/poeOff);?>",
  type:'POST',
  dataType:'text',
  success: function(returnDataFromController) {
    htmlstring = "<h2>Port POE Status: Port " + $('#port').val() + "</h2><p><p>" + returnDataFromController + "<button class='btn' name='enable' id='enable'>Enable POE</button>&nbsp;<button class='btn' name='disable' id='disable'>Disable POE</button>";    

    alert(htmlstring);
    $('#clientajaxcontainer').html(htmlstring); 
    console.log(htmlstring);
}
  });
  //re-enable the buttons.
  $(this).removeAttr("disabled");
  $('#enable').removeAttr("disabled");
});    

</script>

Can you tell me where I'm going wrong? Thanks.

  • 写回答

1条回答 默认 最新

  • dp198879 2012-09-07 20:50
    关注

    The problem is that you are not creating a "live" binding for your buttons and once you call $('#clientajaxcontainer').html(htmlstring); you replace the buttons with new elements which do not have a click binding. Try this instead:

    $('#enable').live('click', function()  {
        //...
    }
    
    $('#disable').live('click', function()  {
        //...
    }
    

    If you're using jQuery 1.8 you can use the on() method instead of live(). That would look like this:

    $(document).on('click', '#enable', function()  {
        //...
    }
    
    $(document).on('click', '#disable', function()  {
        //...
    }
    

    Here's the relevant documentation:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助