我想先加载图像加载器<div id="ajax-content-container">
,然后数据内容将显示在同一div div <div id="ajax-content-container">
使用ajax。然而,图像加载器甚至没有出现,内容直接显示,没有加载图像加载器。如果我在beforeSend下放置返回false,它会显示反向并且负载不会停止。Ajax加载图像在ajax内容加载之前未显示;图像和内容加载在相同的div
beforeSend: function(){
$('#ajax-content-container').html('<img src="<?php echo base_url()?>/img/ajax-
loader.gif" width="200" height="200" style="margin-left:450px; margin-top:300px;" />');
return false;
}`
如何完成此操作?这是我的代码。谢谢您的帮助。
查看:
<?php if (!isset($ajax_req)): ?>
<div class="row">
<div class="span3"> <?php echo
form_dropdown('courses',$dropdown,$selected_value,'id="select_id"');?> </div>
<div class="span3"> <?php echo
form_dropdown('schedule',$dropdown_sched,$date_value,'id="date_option"');?> </div>
<div class="span2"> <?php echo
form_dropdown('status',$dropdown_status,$status_value,'id="status_id"');?></div>
</div>
<?php endif; ?>
<div id="ajax-content-container">
<table class="table table-bordered table-condensed table-striped table-hover">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Phone Number</th>
<th>Status</th>
</tr>
<?php foreach ($student_list as $key=>$value): ?>
<tr>
<td><?php echo $value->first_name; ?></td>
<td><?php echo $value->last_name; ?></td>
<td><?php echo $value->email; ?></td>
<td><?php echo $value->phone_no; ?></td>
<td><?php echo $value->status; ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
的javascript:
<script type="text/javascript">
$(document).ready(function() {
ajax_courses();
ajax_sched();
ajax_status();
});
function ajax_courses() {
$('#select_id').change(function() {
var course_id = $("#select_id").val();
var postData = {'course_id':course_id};
var timeout;
$.ajax({
url: "<?php echo base_url(); ?>/ajax_student_controller/get_ajax_course_student/",
async: false,
type: "POST",
data: postData,
dataType: "html",
beforeSend: function(){
$('#ajax-content-container').html('<img src="<?php echo base_url()?>/img/ajax-
loader.gif" width="200" height="200" style="margin-left:450px; margin-top:300px;"
/>');
},
success: function(data) {
$('#ajax-content-container').html(data);
},
})
});
}
</script>
加载图片仍然不存在。也许它运行速度非常快。我检查了图像路径,但它的正确性。 – Eli
查看我的编辑答案我已经添加了'setTimeout' 3秒,如果它是快速响应,但它不是一个好主意,因为使用'setTimeout'因为如果你的请求需要5秒完成,然后再3秒延迟将被添加,因为许多服务器缓慢/快速取决于主机 –
有什么我可以用来显示加载图像?我已经尝试过使用setTimeout,fadeOut等。也许,我只是把它放在一个错误的方式。 – Eli