选中复选框时如何调用ajax url

时间:2011-05-29 21:34:10

标签: jquery

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

该URL是我的应用程序的本地URL,并将返回一个json响应。勾选复选框后,我想调用URL并传递参数。例如/return/students?q=someparm

2 个答案:

答案 0 :(得分: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)
});

答案 1 :(得分: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/hide/

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