2011-05-29 19 views
0

使用jQuery调用URL的最佳方式是什么通过ajax并使用返回的数据更新<ul>列表?在从url中获取数据的同时,我还想显示一个“加载”微调器。如何选择复选框时调用ajax url

该URL对我的应用程序是本地的,并且会返回一个json响应。当复选框被选中时,我想调用URL并传递一个参数。例如/return/students?q=someparm

回答

1

应尽可能与change事件
的代码可能看起来在某种程度上是这样的:

$('#checkboxId').change(function() { 
    $.ajax({ 
     url: "/return/students?q=someparm", 
     success: function(data){ 
     // modify list based on data 
     } 
    }); 
    // code to show loading spinner (is executed instantly after ajax request, 
    // not waiting for success to be executed) 
}); 
+0

我应该使用$阿贾克斯()函数? – curtis 2011-05-29 21:44:49

+0

如何显示加载微调器图像以获取请求所需的时间? – curtis 2011-05-29 21:45:53

+0

取决于您的加载微调器的外观。例如,它可能是一个默认不可见的GIF图像,您可以在加载时将其设置为可见 – 2011-05-29 21:50:50

0
<span class="spinner" style="display: none"><img src="spinner.gif" /></span> 

$('#checkboxId').change(function() { 
    $('spinner').show(); 
    $.ajax({ 
     url: "/return/students?q=someparm", 
     success: function(data){ 
     // modify list based on data 
     }, 
     complete: function(){ $('spinner').hide(); } 
    }); 
    // code to show loading spinner (is executed instantly after ajax request, 
    // not waiting for success to be executed) 
}); 

​​

http://api.jquery.com/show/