2016-08-03 94 views
1

我正試圖調用腳本上的按鈕單擊..此處函數summarydata不會調用。腳本按鈕單擊

此外,當我檢查控制檯沒有一個單一的錯誤。哪裏有問題?任何解決方案,請

<button id="chartid" type="button" runat ="server">Show</button> 

CODE

[WebMethod] 
     public static string summarydata() 
     { 

      try 
      { 
       T1 sd = new T1(); 
       var data = new TrackDataEntities1().spsumdata().Select(s => new { name = s.Month, data = new int[] { s.data.Value } }).ToArray(); 
       return Newtonsoft.Json.JsonConvert.SerializeObject(data); 

      } 
      catch (Exception) 
      { 
       throw new Exception(); 
      } 

     } 

修訂

<script type="text/javascript"> 
      alert("iooooooooooooo"); 
      $(function() { 
       $('[ID*=chartid]').on('click', function() { 
        alert("i"); 
        $.ajax({ 
         type: "POST", 
         url: "WebForm1.aspx/summarydata", 
         contentType: "application/json; charset=utf-8", 
         dataType: "json", 
         async: true, 
         cache: false, 
         success: function (result) { 
          alert(result.d); 
          alert("i"); 
         }, 

         error: function (error) { 

          alert(error); 
         } 

        }); 
       }); 

</script> 

,當我在Web方法設置斷點,然後點擊按鈕,然後斷點不叫

+0

廣場內'$(文件)。就緒()的'click'事件;' – Pugazh

+0

@OP看到我的回答 – jackjop

回答

3

你的js代碼應該是在$(document).ready()

<script type="text/javascript"> 
      alert("iooooooooooooo"); 
$(document).ready(function(){ 
      $("#chartid").click(function() { 
        alert("i"); 
        $.ajax({ 
         type: "POST", 
         url: "WebForm1.aspx/summarydata", 
         contentType: "application/json; charset=utf-8", 
         dataType: "json", 
         async: true, 
         cache: false, 
         success: function (result) { 
          alert(result.d); 
          alert("i"); 
         }, 

         error: function (error) { 

          alert(error); 
         } 

        }); 
       }); 
}); 

</script> 
+0

我也嘗試這一點,但沒有發生 – user6628729

+0

檢查更新,請@Haresh Vidja – user6628729

0

您是否嘗試過其他選擇,如使用。對()這樣一個

<script type="text/javascript"> 
alert("iooooooooooooo"); 
$(document).ready(function(){ 
     $("#chartid").on("click",function() { 
       alert("i"); 
       $.ajax({ 
        type: "POST", 
        url: "WebForm1.aspx/summarydata", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        async: true, 
        cache: false, 
        success: function (result) { 
         alert(result.d); 
         alert("i"); 
        }, 

        error: function (error) { 

         alert(error); 
        } 

       }); 
      }); 
}); 

</script> 
+0

檢查更新,請 – user6628729

0

裏面的文件準備好地點/包括body標籤結束前的腳本/使用的$(document)。在(」點擊「,」#chartid「,function(){})。因爲它會從根文檔對象搜索帶ID的元素作爲chartid。

+0

請檢查更新 – user6628729

0

您的按鈕可能會觸發回發。如果是,它會導致你有問題。您可以添加return false點擊功能遵循解決此問題:

$("#chartid").on("click",function() { 
    $.ajax({ 
     type: "POST", 
     url: "WebForm1.aspx/summarydata", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     async: true, 
     cache: false, 
     success: function (result) { 
     alert(result.d); 
     alert("i"); 
     }, 
     error: function (error) { 
     alert(error); 
     } 
    }); 
    return false; 
}); 

如果仍然無法正常工作,只是改變按鈕:

<a id="chartid" href="#" runat ="server">Show</button> 

如果你不改變代碼chartid在後面,你不需要runat="server"

<a id="chartid" href="#">Show</button>