这是我的jquery代码,用于在ajax中调用一个开关表示。它可以工作,但它需要大约20秒来加载,所以我想添加一个加载gif等待(当XHR)。我不知道如何处理.load()方法,我刚刚看到了$.ajax()方法。如果有人能解释一下如何用我的方法显示gif,或者把我的代码翻译成$.ajax()方法。
$(document).ready(function() {
$('input:checkbox').change(function(){
var nom = $(this).attr("value");
if ( $(this).is(':checked') ) {
var target_url = "cgi-bin/switch.pl?param=" + $(this).attr('value');
$('<div id="target_' + $(this).attr('value')+ '"></div>').load(target_url).appendTo($('#target'));
}
else {
$('div#target_' + $(this).attr('value')).remove();
}
});谢谢,
再见。
发布于 2011-05-02 16:02:40
你只需要对你的代码做一个小的修改:
$('<div id="target_' + $(this).val() + '"></div>')
.html('<img src="ressources/loading.gif" />')
.load(target_url)
.appendTo('#target');在您的原始代码(现已编辑)中,您将创建两个独立的div。
发布于 2011-05-02 15:55:50
$.get()就是您想要的,只是$.ajax()的一个简短别名
$(document).ready(function() {
$('input:checkbox').change(function(){
var nom = $(this).attr("value");
if ( $(this).is(':checked') ) {
var target_url = "cgi-bin/switch.pl?param=" + $(this).attr('value');
$.get(target_url, function(data){
$('#target').html(data);
});
}
else {
$('div#target_' + $(this).attr('value')).remove();
}
});发布于 2011-05-02 15:55:56
你仍然可以用.load()做到这一点。您只需在调用之前放置一个加载指示器,然后通过回调.load()方法(在我的示例中为hideLoadingIndicator)来隐藏加载指示器。
if ( $(this).is(':checked') ) {
var target_url = "cgi-bin/switch.pl?param=" + $(this).attr('value');
//put your loading indicator code here
$('<div id="target_' + $(this).attr('value')+ '"></div>')
.load(target_url, {}, hideLoadingIndicator)
.appendTo($('#target')); //note callback
..
..
function hideLoadingIndicator() {
//code to hide loading indicator
}https://stackoverflow.com/questions/5854601
复制相似问题