2011-05-29 18 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/