2013-08-26 31 views
0

我正在做asp.net中的jquery ajax調用,我從數據庫中獲取一些值在文本框中,並基於它我正在jquery progressbar。 它在文本框中獲取值,但它並沒有取得進度條的第一次值,我不得不重新加載頁面進度條的值。
下面是我的代碼jquery在asp.net中的Ajax調用進度條更新

$(function() { 
     GetValue(); 
     var l_count= parseInt($("#txtcount").val()); 

     $("#sliderlicense").progressbar({ 
      max: 100, 
      value: l_count 
     });   
    }); 

    function GetValue() { 
     $.ajax({ 
      type: "POST", 
      url: "MyPage.aspx/GetCount", //url to point your webmethod  
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (Result) { 
       $("#txtcount").val(Result.d); 
      }, 
      error: function() { alert('error'); } 
     }); 
    } 


[System.Web.Services.WebMethod()] 
     public static string GetCount() 
     { 
      //Get values from DB and return it 
     } 

我也試圖與的document.ready,但沒有運氣,我應該用哪個事件的jQuery做的第一次我的進度條。

回答

1

試試這個:

異步:假,加入AJAX調用

$(document).ready(function(){ 
     GetValue(); 
     var l_count= parseInt($("#txtcount").val()); 

     $("#sliderlicense").progressbar({ 
      max: 100, 
      value: l_count 
     }); 

    }); 
}) 
function GetValue() { 
    $.ajax({ 
     type: "POST", 
     url: "MyPage.aspx/GetCount", //url to point your webmethod  
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     async:false, 
     success: function (Result) { 
      $("#txtcount").val(Result.d); 
     }, 
     error: function() { alert('error'); } 
    }); 
} 
+0

由於它的工作完美:) –

1

AJAX意味着Asynchronous,這麼說,意味着你需要等待,直到你的第一個請求suceed,並與pocess後值。

一些僞代碼:

$.ajax({ 
     type: "POST", 
     url: "MyPage.aspx/GetCount", //url to point your webmethod  
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (Result) { 
      $("#txtcount").val(Result.d); 

      //HERE ASSIGN TO PROGRESS BAR 
      var l_count= parseInt($("#txtcount").val()); 

      $("#sliderlicense").progressbar({ 
      max: 100, 
      value: l_count 
      }); 
      // -------------------------- 
     }, 
     error: function() { alert('error'); } 
    });