我有一个包含不同部分的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单击事件处理程序函数的完整版本:
$('.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调用的成功部分中.而现在..。他们似乎起作用了。有人能解释一下为什么会这样吗?
发布于 2012-10-01 15:33:27
我不确定它是否与您的问题有关,但是ID必须是唯一的,并且每个对象都有一个#deleteRecord元素。
您应该将代码更改为以下内容:
<td><button class='btn delete'>Delete</button></td>
...
$('.delete').live('click', function() {编辑:在最后非常明显(根据您的最新编辑):您正在显示进度指示器并立即隐藏它,而它刚刚开始淡入.
https://stackoverflow.com/questions/12676245
复制相似问题