警报框中的数据通过函数javascript

时间:2016-06-01 08:15:38

标签: javascript jquery html

参考this question,这就是在没有函数的情况下解决这个问题的方法。 现在我有一个函数,我尝试调用javascript并尝试在alert框中调用该数据,但alert框不会填充。我的代码是这样的:

$('#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>

2 个答案:

答案 0 :(得分: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>

以上代码正常运作。检查并更新您的。确保按钮类型按钮。

答案 1 :(得分: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>