我有一个 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> </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
.
单击“getlist”按钮时,它会显示。但是,当单击“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> </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 调用的成功部分中......现在......它们似乎正在工作。谁能解释为什么会这样?