2016-06-01 49 views
0

參考this question,這是沒有函數的解決方法。 現在我有一個函數,我嘗試在javascript中調用,並嘗試在alert框中調用該數據,但alert框未填充。我的代碼是這樣的:數據通過函數提醒框中的數據javascript

$('#submitchart').click(function() { 
 
    //alert("i"); 
 
    var webmethod = 'WebForm1.aspx/Jqufunc'; 
 
    $.ajax({ 
 
    type: 'POST', 
 
    url: webmethod, 
 
    data: JSON.stringify({ 
 
     yearP: $('#yearValue').val() 
 
    }), 
 
    contentType: 'application/json;charset=utf-8', 
 
    dataType: 'json', 
 
    success: function(response) { 
 
     debugger; 
 
     alert(JSON.stringify(response.d)); 
 
     var data1 = response.d.split('*')[0]; 
 
     var data2 = response.d.split('*')[1]; 
 
     alert(data1); 
 
     alert(data2); 
 
    }, 
 
    error: function() { 
 
     debugger; 
 
     alert(data1); 
 
     alert(data2); 
 
    } 
 
    }); 
 
}); 
 

 
function Loadchart() { 
 
    $('#submitchart').click(); 
 
};
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
 

 
<form id="form1" runat="server"> 
 
    <asp:DropDownList ID="yearValue" runat="server" AutoPostBack="True" ViewStateMode="Enabled"></asp:DropDownList> 
 
    <button id="submitchart" runat="server">Show Chart</button> 
 
    <div id="container" style="width: 100%; height: 400px;"></div> 
 
</form>

+0

是瀏覽器停止在調試器? – brk

+0

刪除JSON.stringify – madalinivascu

+0

你能提供響應的格式嗎? – madalinivascu

回答

0
<head runat="server"> 
    <title></title> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <select id="yearValue"><option>1920</option></select> 
     <button id="submitchart" runat="server" type="button">Show Chart</button> 
     <div id="container" style="width: 100%; height: 400px;"></div> 
    </form> 
</body> 

<script type="text/javascript"> 
    $('#submitchart').click(function() { 
     //alert("i"); 
     var webmethod = 'test.php'; 
     $.ajax({ 
      type: 'POST', 
      url: webmethod , 
      data: JSON.stringify({ 
       yearP: $('#yearValue').val() 
      }), 
      contentType: 'application/json;charset=utf-8', 
      dataType: 'json', 
      success: function(response) { 
       alert('working'); 

      }, 
      error: function() { 
       alert('error'); 
      }  
     }); 
    }); 

    function Loadchart() { 
     $('#submitchart').click(); 
    }; 
</script> 

上面的代碼是工作。檢查並更新你的。確保按鈕類型按鈕。

0

裹在文檔準備好聲明你的代碼,刪除JSON.stringify

<head runat="server"> 
    <title></title> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <select id="yearValue"><option>1920</option></select> 
     <button id="submitchart" runat="server" type="button">Show Chart</button> 
     <div id="container" style="width: 100%; height: 400px;"></div> 
    </form> 
</body> 

<script type="text/javascript"> 
    $(function(){ 
    $('#submitchart').click(function (e) { 
     e.preventDefault(); 
     var webmethod = 'test.php'; 
     $.ajax({ 
      type: 'POST', 
      url: webmethod , 
      data: JSON.stringify({ 
       yearP: $('#yearValue').val() 
      }), 
      contentType: 'application/json;charset=utf-8', 
      dataType: 'json', 
      success: function(response) { 
       alert('working'); 

      }, 
      error: function() { 
       alert('error'); 
      }  
     }); 
    }).trigger('click'); 
}); 
</script>