2017-01-14 33 views
0
<asp:CheckBox runat="server" ID="checkbox1" /> 

每当我选中或取消我的ASP复选框,它需要触发JavaScript中的事件,但它似乎像。点击功能不会被触发。 .click事件是否错误?ASP复选框在Javascript中触发一个事件时,它被点击

$(document).ready(function() { 
    $("#checkbox1").click(function (e) { 
     if (this.checked) { 
      //do something 
     } 
     else { 

     } 
    }); 
}); 
+0

检查您是否使用母版页,然后id“checkbox1”将不起作用,您必须给该元素一些类,并在JavaScript中使用该类选择器。 –

回答

0

你可能需要改变

$("#checkbox1").click(function (e) { 

$("#<%= checkbox1.ClientID %>").click(function (e) { 

asp.net重命名控件的ID以确保没有重复。如果你检查HTML源代码,可能看起来像这样:ContentPlaceHolder1_checkbox1。这就是为什么jQuery无法找到它。

+0

它的工作原理!非常感谢 ! :d – Student

0

我希望它能为你工作。 对于动态创建的element,您必须使用event delegation,如果您想通过属性进行选择,您可以使用attribute equals selector

$(document).on("change", "input[name='member']", function() { 
       alert("CheckBox Changed."); 
       if (this.checked) {alert("CheckBox checked.");} 
       else{alert("CheckBox not checked.");} 
      }); 

OR

$(document).on("change", "$('#<%=checkbox1.ClientID%>')", function() { 
       alert("CheckBox Changed."); 
       if (this.checked) {alert("CheckBox checked.");} 
       else{alert("CheckBox not checked.");} 
      }); 

这里是链接,通过这个链接 JS Fiddler

相关问题