繁体   English   中英

单击按钮上的脚本

[英]Script on button click

我试图在按钮点击时调用脚本..这里函数summarydata不调用。

另外,当我检查控制台时,没有一个错误。 问题出在哪儿? 有解决方案吗?

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

 [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方法上设置断点并单击按钮时,断点不会调用

你的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>

您是否尝试过像使用.on()这样的其他选项

<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>

在body标记结束之前放置文档ready / include脚本/使用$(document).on(“click”,“#chartid”,function(){})而不是.click。 因为它将从根文档对象中搜索ID为chartid的元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM