2012-07-03 32 views
1

在ASP.NET MVC中,我使用JSON和jquery獲取圖表數據。有沒有一種方法來禁用控件,並設置光標等待數據加載和圖表繪製? 我的代碼抽象爲獲取Json期間禁用控件

<script type="text/javascript"> 
    $(document).ready(function() { 
      $.getJSON(
       '@Url.Action("JsonPlot", "Home")', 
       function (chartData) { 
        var plot1 = $.jqplot('chartdiv', [points], {...}); 
      });    
     }); 
</script> 

我在哪裏可以把

$("*").attr("disabled", "disabled"); 
$('body').css('cursor', 'wait'); 

然後

謝謝!

+1

爲什麼'$( 「*」)'?通用選擇器太慢了。 – undefined

+0

@undefined謝謝!此外,'$(「*」)'沒有正常工作,所以我縮小到'$(「輸入」),$(「選擇」)' – Dmitry

回答

1

要充分利用jQuery的AJAX回調,你應該使用$.ajax

$.ajax({ 
    url: '@Url.Action("JsonPlot", "Home")', 
    dataType: 'json', 
    beforeSend: function() { 
     /* Disable UI */ 
    }, 
    success: function(data) { 
     /* Update UI using server response */ 
    }, 
    complete: function() { 
     /* Enable UI again, regardless of whether 
      server call was successful or not*/ 
    }, 
    error: function(jqXHR, textStatus) { 
     /* Display error message to user */ 
    } 

}); 
+0

謝謝,我工作得很好! – Dmitry

2
$(document).ready(function() { 
$("*").attr("disabled", "disabled"); 
$('body').css('cursor', 'wait'); 

      $.getJSON(
       '@Url.Action("JsonPlot", "Home")', 
       success:function (chartData) { 
        var plot1 = $.jqplot('chartdiv', [points], {...}); 
        $("*").removeAttr('disabled'); 
        $('body').css('cursor', 'auto'); 

      });    
     }); 
0

您可以使用jQuery方法:ajaxStart()和ajaxStop()來。