2013-03-29 79 views
0

我試圖在asp.net button中激發一個jquery,它沒有被觸發。哪裏出錯了?jQuery與Asp.net按鈕

//代碼

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

能有人幫幫我嗎?

+1

$(文件) - >右括號缺少 –

+0

更新您的jQuery的版本也可能的話 –

+0

這是'$(文件).ready(function(){// ...});' – Anujith

回答

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

試試這個:

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

在腳本中定義的功能函數名():

<script type ="text/javascript"> 

     function function_name() { 
      //what ever you want. 
      } 
</script> 
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>