0

我试图在 asp.net 按钮中触发 jquery。它没有被触发。出了什么问题?

//代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src ="Scripts/jquery-1.4.1.js"></script>
<script type ="text/javascript">
  $(document.ready(function(){
       $("#<%=Button1.ClientID %>").click(function () {
                 showalert('HTML Button Clicked');
       });
  });

  function showalert(btnText) {
      alert(btnText)
  } 
</script>

</head>
<body>
   <form id="form1" runat="server">
     <div>
        <asp:Button ID="Button1" runat="server" Height="41px" 
           style="margin-left: 340px; margin-top: 197px" 
          Text="Button" Width="147px"/>
     </div>
  </form>
</body>
</html>

有人能帮我吗?

4

3 回答 3

1

试试这个:

  <asp:Button ID="Button1" runat="server" Height="41px" OnClientClick="function_name();" 
             style="margin-left: 340px; margin-top: 197px" Text="Button" Width="147px"/>

在脚本中定义函数 function_name():

<script type ="text/javascript">

         function function_name() {
             //what ever you want.
            } 
</script>
于 2013-03-29T06:19:21.573 回答
1

您的文档就绪语法错误。试试这个代码

<script type ="text/javascript">

    $(document).ready(function () {
        $("#<%=Button1.ClientID %>").click(function () {

            showalert('HTML Button Clicked');
        });
    });
  function showalert(btnText) {
      alert(btnText)
  } 
</script>

</head>
<body>
   <form id="form1" runat="server">
     <div>
        <asp:Button ID="Button1" runat="server" Height="41px" 
           style="margin-left: 340px; margin-top: 197px" 
          Text="Button" Width="147px"/>
     </div>
  </form>
</body>
</html>
于 2013-03-29T06:29:12.133 回答
1

)在 document.ready中丢失了

<script type ="text/javascript">
  $(document).ready(function(){
       $("#<%=Button1.ClientID %>").click(function (event) {
             showalert('HTML Button Clicked');
             event.preventDefault();//don't forgot this
       });
  });

 function showalert(btnText) {
    alert(btnText)

  } 
</script>
于 2013-03-29T06:29:37.297 回答