带有toggle()的复选框会打破选中/未选中状态 [英] Checkbox with toggle() breaking the checked/unchecked state

查看:140
本文介绍了带有toggle()的复选框会打破选中/未选中状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能散些光吗?

为什么此.toggle()打破了复选框的选中/未选中状态?

Why this .toggle() is breaking the checked/unchecked state of a checkbox?

$('#checkbox').toggle(function(){},function(){});​

当我想到的时候,它什么也没做.

when I think, it's just doing nothing.

这取自对此 answer

推荐答案

toggle()用两个回调调用会在目标元素上添加一个click事件.该事件将在偶数点击时执行第一个回调;第二个是奇数.

toggle() called with two callbacks adds a click event on the targeted element. The event will execute the first callback on even clicks; the second one on odd ones.

而且,这就是为什么改变行为的原因,根据手册:

Also, and this is why it changes the behaviour, according to the manual:

该实现还会在事件上调用.preventDefault(),因此,如果在元素上调用了.toggle(),则不会跟随链接,也不会单击按钮.

The implementation also calls .preventDefault() on the event, so links will not be followed and buttons will not be clicked if .toggle() has been called on the element.

这篇关于带有toggle()的复选框会打破选中/未选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆