jQuery的单击事件处理程序的复选框叫了两声 [英] jquery click event handler is called twice for a checkbox
本文介绍了jQuery的单击事件处理程序的复选框叫了两声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有在ASPX页面下面的code的一个问题:
I have a problem with the following code in an ASPX page:
<script type="text/javascript">
$(document).ready(function() {
$('.test').click(function() {
alert("click")
})
});
</script>
<asp:CheckBox runat="server" Text="Checkbox XYZ" CssClass="test" ID="cb1" />
在浏览器(FF3.5 / IE8)我有以下问题:
In the browser (FF3.5 / IE8) I have the following problem:
- 如果我点击复选框(小广场),它按预期工作
- 如果我点击复选框的文本(复选框XYZ),然后点击事件被触发两次,警报显示两次
我想这与该复选框被渲染到HTML的方式,这是这样做的:
I guess this has to do with the way the checkbox is rendered to HTML, which is like this:
<span class="test">
<input id="ctl00_c1_cb1" type="checkbox" name="ctl00$c1$cb1" checked="checked"/>
<label for="ctl00_c1_cb1">Checkbox XYZ</label>
</span>
我如何正确设置,单击事件处理程序,以prevent它被调用两次?
How do I correctly setup the click event handler to prevent it from being called twice?
推荐答案
重读我的问题后,好了,我找到了一种方法如何解决这个问题。
Well after reading my question again, I found a way how to solve it.
就输入添加到jQuery选择:
Just add "input" to the jQuery selector:
<script type="text/javascript">
$(document).ready(function() {
$('.test input').click(function() {
alert("click")
})
});
</script>
这篇关于jQuery的单击事件处理程序的复选框叫了两声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文