单击标签时 jQuery Click 会触发两次

标签 jquery

我正在使用 jQuery 创建自定义单选按钮,但遇到了问题。 当单击与 radio 关联的标签时,单击事件会触发两次,如果我仅单击 radio 本身,则它工作正常(实际上,我单击的不是 radio ,而是包装整个输入和标签的 div)。这是代码:

HTML:

 <div id="box">
     <asp:RadioButtonList ID="RadioButtonList1" runat="server">
         <asp:ListItem>RADIO1</asp:ListItem>
         <asp:ListItem>RADIO2</asp:ListItem>
         <asp:ListItem>RADIO3</asp:ListItem>
     </asp:RadioButtonList>
</div>

jQuery:

<script type="text/javascript">
       $(function () {
            $('#box').find('input:radio').each(function (i) {

            var input = $(this);
            // get the associated label using the input's id
            var label = $('label[for=' + input.attr('id') + ']');
            // wrap the input + label in a div
            $('<div class="custom-radio"></div>').insertBefore(input).append(label, input);

            var wrapperDiv = input.parent();

            // find all inputs in this set using the shared name attribute
            var allInputs = $('input[name=' + input.attr('name') + ']');

            // necessary for browsers that don't support the :hover pseudo class on labels
            label.hover(

            function () {
                $(this).addClass('hover');
            }, function () {
                $(this).removeClass('hover checkedHover');
            });

            //bind custom event, trigger it, bind click,focus,blur events
            wrapperDiv.bind('updateState', function () {
                if ($(this)[0].children[1].checked) {
                    allInputs.each(function () {
                        var curDiv = $('div > label[for=' + $(this).attr('id') + ']').parent();
                        curDiv.removeClass('custom-radio-checked');
                        curDiv.addClass('custom-radio');
                    });
                    $(this).toggleClass('custom-radio custom-radio-checked');
                }
                else {
                    $(this).removeClass('custom-radio-checked checkedHover checkedFocus');
                }

            })
            .trigger('updateState')
            .click(function () { console.log('click'); })
            .focus(function () {
                label.addClass('focus');
            }).blur(function () {
                label.removeClass('focus checkedFocus');
            });
        }); 
       });
   </script>

是否有针对此行为的任何解决方案?

最佳答案

我尝试通过添加来添加上面的解决方案:

evt.stopPropagation();
evt.preventDefault();

但是没用。但是添加这个:

evt.stopImmediatePropagation();

问题解决了! :)

关于单击标签时 jQuery Click 会触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8238599/

相关文章:

JavaScript 下载到 Excel 表格

javascript - 如果文本输入字段是可选的,我应该如何将它的值从 javascript 传递到 php

javascript - 禁用零作为 &lt;input&gt; 中的第一个字母

jquery - 使用 Partial 加载 KendoUI Tabstrip

javascript - 带有选择器作为参数的 jQuery 函数

jquery - 将 jQuery Mobile radio 从水平转换为垂直

javascript - simpleWebRTC 与 php 后端

javascript - 在不重新加载页面的情况下在 url 中 append 参数

javascript - html5 Canvas 工具提示仅对最后绘制的对象可见,对以前的对象不可见

javascript - 将点击事件绑定(bind)到动态创建的按钮,无需选择器