I have asp.net radio button control in my application, I want to apply jquery click event on this control with class name. I have set CssClass property and made script with the class name,but its not working.When I inspect the HTML in firebug,its making the span with the CSSClass I have provided This is my aspx code
我在我的应用程序中有asp.net单选按钮控件,我想在类控件上使用类名来应用jquery click事件。我已经设置了CssClass属性并使用类名创建了脚本,但它不起作用。当我在firebug中检查HTML时,它使用我提供的CSSClass创建了span这是我的aspx代码
<asp:RadioButton ID="radioyes" CssClass="quest" GroupName="Ques" runat="server" Text="Yes" />
<asp:RadioButton ID="radiono" CssClass="quest" GroupName="Ques" runat="server" Text="No" />
its rendering in browser as
它在浏览器中呈现为
<span class="quest"><input type="radio" value="quest2yes" name="ctl00$ContentBody$Wizard1$Q2" id="ctl00_ContentBody_Wizard1_radioyes"><label for="ctl00_ContentBody_Wizard1_radioyes">Yes</label></span>
<span class="quest"><input type="radio" value="quest2no" name="ctl00$ContentBody$Wizard1$Q2" id="ctl00_ContentBody_Wizard1_radiono"><label for="ctl00_ContentBody_Wizard1_radiono">No</label></span>
When I am typing $(".quest").length
in console,its showing 0
as well.
当我在控制台中键入$(“。quest”)。length时,它也显示为0。
I am trying with this
我正在尝试这个
$(".quest").click(function(){
});
I also tried with
我也尝试过
$('#<%=radioyes.ClientID%>').click(function () {
});
but still not working,its not even showing any error in console
但仍然没有工作,它甚至没有在控制台中显示任何错误
2 个解决方案
#1
1
Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .click()
事件处理程序仅绑定到当前选定的元素;它们必须存在于您的代码调用.click()时页面上
You are assigning event handler to element before it actually gets loaded in DOM.
您在将元素实际加载到DOM之前为元素分配事件处理程序。
So you must use event delegation as,
所以你必须使用事件委托,
$("document").on("click", ".quest", function () {
// rest code here
});
.on()
#2
4
You might be using the code before the dom element actually loaded to the page.
您可能在dom元素实际加载到页面之前使用代码。
Wrap your function inside $(document).ready(function ()
将您的函数包装在$(document).ready(function()中
$(document).ready(function () {
$(".quest").click(function () {
alert('s');
});
});
#1
1
Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .click()
事件处理程序仅绑定到当前选定的元素;它们必须存在于您的代码调用.click()时页面上
You are assigning event handler to element before it actually gets loaded in DOM.
您在将元素实际加载到DOM之前为元素分配事件处理程序。
So you must use event delegation as,
所以你必须使用事件委托,
$("document").on("click", ".quest", function () {
// rest code here
});
.on()
#2
4
You might be using the code before the dom element actually loaded to the page.
您可能在dom元素实际加载到页面之前使用代码。
Wrap your function inside $(document).ready(function ()
将您的函数包装在$(document).ready(function()中
$(document).ready(function () {
$(".quest").click(function () {
alert('s');
});
});