jquery:进度指示器无法使用ajax调用正确显示/隐藏

时间:2012-10-01 15:25:06

标签: php jquery html ajax

我有一个html页面,上面有不同的部分(通过DIV标签完成)。 使用ajax调用创建/更新这些部分。其中一个div实际上是一个进度指示器 - 它只显示一个图像,告诉用户我正在后台处理他们的请求。

我的问题是,在请求时并不总是显示此进度指示器,即使每个单击事件处理器中的前几行代码都有显示此部分的代码。 (然后在每个按钮的末尾单击我隐藏div)

这是我的代码的基本框架:

 <div class="row-fluid">        
  <div class="span12">

      <h2>Title for Page></h2>
          <table class="table table-bordered table-striped"  id="assignedRecords">
          <thead>
              <tr>                  
                  <th>ID</th>
                  <th>Name</th>
                  <th>Status</th>
                  <th>Voice</th>
                  <th>Jumbo</th>
                  <th>Mode</th>
                  <th>&nbsp;</th>
              </tr>
          </thead>
          <tbody>

              <?php foreach ($databaserecords as $record): ?>
                  <tr>          
                                        ...  code to create initial table
                                        <td><button class='btn deleteBtn'>Delete</button></td>
                </tr>
              <?php endforeach ?>

          </tbody>
          </table>

          <div id="progress-indicator">
                 <img src="<?php echo base_url();?>assets/img/wip.gif"> Retrieving records...</img>
          </div>    

          <button class="btn" id="getlist">Get List of Available Objects</button>
  </div>

  <div class="span12" id="availableObjectsAjaxContainer"></div> 
  <!--this is updated via ajax when the user clicks on the modify button. -->

 </div>   


  <script>

   $(document).ready(function(){

    //hide the please wait div by default.
    $('#progress-indicator').hide();


            $('#getlist').live('click', function()  {


                    $('#progress-indicator').html("<img src='" + BASEPATH + "assets/img/wip.gif' /> Retrieving...</img>");
                    $('#progress-indicator').fadeIn();      
                    .
                   // updates availableObjectsAjaxContainer section
                    .
                    .

    $('#deleteRecord').live('click', function()  {

                $('#progress-indicator').html("<img src='" + BASEPATH + "assets/img/wip.gif' /> deleting record...</img>");
                $('#progress-indicator').fadeIn();      
                .
                .
                // updates assignedRecords table section
                .

单击“获取列表”按钮时,会显示该按钮。但是,单击“deleteRecord”按钮时,不会显示该按钮。 我已经确保我的ajax调用不会意外地“消灭”具有进度指示器的div。我的ajax调用只更新“assignedRecords”表部分......和“availableObjectsAjaxContainer”部分。

你能告诉我哪里出错吗?

感谢。

编辑1

以下是deleteRecord click事件处理函数的完整版本:

  $('.deleteBtn').live('click', function()  {
                $('#progress-indicator').html("<img src='" + BASEPATH + "assets/img/wip.gif' /> deleting vlan...</img>");
                $('#progress-indicator').fadeIn();          
                //get a count of all records. only allowed to delete if you have more than one record.

                var reccount = $('#assignedRecords tbody tr').length;

                if (reccount > 1)
                {

                        var userSelectionId  = $(this).parent().siblings('.id').text(); 
                        var modeDelete = $(this).parent().siblings('.mode').text();


                        var fullpath = BASEPATH + 'index.php/switches/delete/' + userSelectionId + '/' + modeDelete;
                        console.log(fullpath);
                        $.ajax({
                                url:fullpath,
                                type:'POST',
                                dataType:'json',
                                success: function(returnDataFromController) {

                                                console.log(returnDataFromController);
                                                //build table contents 
                                                var htmlstring = '<thead>';  
                                                htmlstring = htmlstring + '<tr>';
                                                htmlstring = htmlstring + '<th>Id</th>';
                                                htmlstring = htmlstring + '<th>Name</th>';
                                                htmlstring = htmlstring + '<th>Status</th>';
                                                htmlstring = htmlstring + '<th>Voice</th>';
                                                htmlstring = htmlstring + '<th>Jumbo</th>';
                                                htmlstring = htmlstring + '<th>Mode</th>';
                                                htmlstring = htmlstring + '<th>&nbsp;</th>';
                                                htmlstring = htmlstring + '</tr>';
                                                htmlstring = htmlstring + '</thead>';
                                                htmlstring = htmlstring + '<tbody>';

                                                //loop through results from ajax call and build table.  
                                                for(i = 0; i < returnDataFromController.length; i++) {
                                                      //alert(returnDataFromController[i].Id);
                                                      htmlstring = htmlstring + "<tr>"
                                                      htmlstring = htmlstring + "<td class ='recordid'>" + returnDataFromController[i].Id + "</td>";
                                                      htmlstring = htmlstring + "<td>" + returnDataFromController[i].Name + "</td>";
                                                      htmlstring = htmlstring + "<td>" + returnDataFromController[i].Status + "</td>";
                                                      htmlstring = htmlstring + "<td>" + returnDataFromController[i].Voice +"</td>";
                                                      htmlstring = htmlstring + "<td>" + returnDataFromController[i].Jumbo + "</td>";
                                                      htmlstring = htmlstring + "<td class='mode'>" + returnDataFromController[i].Mode +"</td>";
                                                      htmlstring = htmlstring + "<td><button class='btn deleteBtn'>Delete</button></td>";
                                                      htmlstring = htmlstring + "</tr>";    

                                                }//end loop

                                               htmlstring = htmlstring + "</tbody>";
                                               htmlstring = htmlstring + "</table>";

                                             $('#assignedRecords').html(htmlstring);
                                  }//end success ajax call

                        });//end ajax.                                               
                }
                else
                {   
                    alert("Must have at least one record!");
                }
                $('#progress-indicator').hide();

  }); //end click event 

编辑2:

    $('#getList').click(function()  {

            $(this).attr("disabled","disabled");
            $('#progress-indicator').html("<img src='" + BASEPATH + "assets/img/wip.gif' /> Retrieving ...</img>");
            $('#progress-indicator').fadeIn();
            $.ajax({
              url:"<?php echo site_url('switches/showavailrecs/'.$ip.'/'.$hardwaremodel);?>",
              type:'POST',
              dataType:'json',
              success: function(returnDataFromController) {

                  var htmlstring;


                  htmlstring = htmlstring + "<th>Id</th><th>Name</th>";

                  //loop through results
                  for(i = 0; i < returnDataFromController.length; i++) {
                          //alert(returnDataFromController[i].Id);
                          htmlstring = htmlstring +  "<tr><td><a href=>"+returnDataFromController[i].Id+"</a></td><td>"+ returnDataFromController[i].Name+"</td></tr>"; 

                  }


                  $('#availableObjectsAjaxContainer').html(htmlstring);

                  $('#progress-indicator').hide();
                 }//end success
              }); //end ajax call
              $(this).removeAttr("disabled");
    });

编辑3:

我想我知道发生了什么 - 虽然我不知道为什么会这样做。

我已将所有我的.hide命令移动到我的ajax调用的成功部分中......现在......它们似乎正在工作。 任何人都可以解释为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

我不确定它是否与您的问题有关,但ID必须是唯一的,并且每个对象都有一个#deleteRecord元素。

您应该将代码更改为:

<td><button class='btn delete'>Delete</button></td>

...

$('.delete').live('click', function()  {

编辑最终显而易见(基于您的最新编辑):您正在显示进度指示器,并在它刚开始淡入时立即将其隐藏...