微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

跨度内的复选框on跨度onclick ..检查输入.. jQuery递归问题

如何解决跨度内的复选框on跨度onclick ..检查输入.. jQuery递归问题

|| 此代码导致递归行为。 我希望能够单击范围中的文本并选中/取消选中子输入并触发子输入的click事件。 http://jsfiddle.net/cwfontan/ALEBr/
        <span class=\"RedHover\">
            <input type=\"checkBox\" name=\"chkShowBusn\" id=\"chkShowBusn\" tabindex=\"9\" />Business
        </span>



$(\'.RedHover\').click(
        function (e) {
            try {
                $(this).children(\'input\').trigger(\'click\');
                e.stopPropagation();
                e.preventDefault();
                console.log(\'here\');
            } catch (e) {
                //handle no click event.alert
                console.log(e.description);
            }
        }
    );
    

解决方法

        
<input type=\"checkbox\" name=\"chkShowBusn\" id=\"chkShowBusn\" tabindex=\"9\" />
<label for=\"chkShowBusn\">Business</label>
而且您甚至不需要jQuery ...     ,        问题是在
input
元素上触发的事件然后冒泡并在
span
上处理。原始事件和以编程方式触发的事件都会发生这种情况。 解决方案可能是检查事件是否起源于
input
元素,如果发生则不进行处理:
$(\'.RedHover\').click(
    function (e) {
        if (e.target.nodeName.toLowerCase() !== \'input\') {
            $(this).children(\'input\').trigger(\'click\');
            e.stopPropagation();
            e.preventDefault();
            console.log(\'here\');
        }
    }
);
“ 6”是不必要的,jQuery将确保事件对象始终存在并具有您使用的方法。 视您的预期行为而定,
stopPropgation
preventDefault
的呼叫可能是不必要的。 由于
span
只有一个孩子,因此您可以采用另一种方法。如果
this
(处理事件的元素,总是
span
)与
e.target
(事件起源的元素,
span
input
)相同,则可以继续:
$(\'.RedHover\').click(
    function (e) {
        if (this === e.target) {
            $(this).children(\'input\').trigger(\'click\');
            e.stopPropagation();
            e.preventDefault();
            console.log(\'here\');
        }
    }
);
请注意,这比检查ѭ16不稳定得多:如果将来更改HTML,则可能会破坏此代码。     ,        您可以像这样单击复选框来停止传播
$(\'input[type=checkbox]\').click(function(e){
    e.stopPropagation();
                e.preventDefault();
});
检查这个jsfiddle     ,        我有一个类似的问题,发现这是解决方案。 的HTML
<span for=\"chkCheckbox\">
    <input id=\"chkCheckbox\" type=\"checkbox\" />
    <label for=\"chkCheckbox\">My Checkbox Label</label>
</span>
Javascript(扩展为jQuery很容易,因为@lonesomeday已回答,所以不包含它,而且我总是更喜欢Javascript而不是使用其他库)
var checkboxSpans = document.querySelectorAll(\"span[for]\");

for (var i = 0; i < checkboxSpans.length; i++) {
    var forCheckbox = checkboxSpans[i].getAttribute(\"for\");
    forCheckbox = document.getElementById(forCheckbox);

    if (forCheckbox != null) {
        checkboxSpans[i].onclick = function (chk) {
            return function (e) {
                if (this === e.target) {
                    e.stopPropagation();
                    e.preventDefault();
                    if (chk.checked != true) {
                        chk.checked = true;
                    } else {
                        chk.checked = false;
                    }
                }
            }
        }(forCheckbox);
    }
}
这样,您可以像使用标签一样使用它。实际上,可以将其添加到任何类型的元素中。 扩展了@lonesomeday提供的答案     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。