绑定禁用后禁用的输入仍然响应鼠标事件

发布于 2024-12-28 05:30:04 字数 475 浏览 0 评论 0原文

我使用 Khavilo Dmitry 的 jQuery 复选框代码 作为复选框和单选按钮。这看起来不错,但他的禁用代码不起作用。正如预期的那样,该按钮呈灰色,但它仍然响应鼠标单击。

我猜测问题在于他绑定禁用如下:

var ch = this; /* Reference to DOM Element*/
...   
$ch.bind('disable', function() {ch.wrapperInner.addClass(settings.cls+'-disabled');})
   .bind('enable',  function() {ch.wrapperInner.removeClass(settings.cls+'-disabled');});

此代码是否删除了任何会禁用鼠标事件的默认绑定?谢谢。

I'm using Khavilo Dmitry's jQuery checkbox code for checkboxes and radio buttons. This looks good, but his disable code doesn't work. The button is grayed out, as expected, but it still responds to mouse clicks.

I'm guessing that the problem is that he binds to disable as follows:

var ch = this; /* Reference to DOM Element*/
...   
$ch.bind('disable', function() {ch.wrapperInner.addClass(settings.cls+'-disabled');})
   .bind('enable',  function() {ch.wrapperInner.removeClass(settings.cls+'-disabled');});

Is this code removing any default binding that would disable mouse events? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气称霸 2025-01-04 05:30:04

代码所做的唯一事情是将函数绑定到事件“disable”和“disable”。 'enable'

当'disable'事件被触发时,他正在向内部跨度添加一个类jquery-checkbox-disabled。他在启用事件中将其删除。

<input name="checkbox.1.2" type="checkbox" id="check" checked="" style="position: absolute; z-index: -1; visibility: hidden; " disabled="disabled">
<span class="jquery-checkbox jquery-checkbox-checked">
  <span class="mark jquery-checkbox-disabled">
    <img src="empty.png">
  </span>
</span>

正如您所看到的,该复选框确实被禁用,但为您提供自定义外观的元素只是仍然可单击的常规跨度。

看起来 Dmitry 正在将这些跨度的单击事件传递给隐藏的复选框。

a.wrapper.click(function(e) {
                b.trigger('click', [e]);
                i(e);
                return false
            });

这就是为什么即使复选框被禁用,您仍然可以获得该复选框的点击事件。正在以编程方式单击该复选框。

因此,如果您的问题是尝试检测复选框何时更改,那么我建议您使用更改事件,因为它似乎没有遇到相同的问题。

http://jsfiddle.net/vW6gj/

The only thing that code is doing is binding functions to the events 'disable' & 'enable'

When the 'disable' event is fired he is adding a class jquery-checkbox-disabled to the inner span. He removes it on the enable event.

<input name="checkbox.1.2" type="checkbox" id="check" checked="" style="position: absolute; z-index: -1; visibility: hidden; " disabled="disabled">
<span class="jquery-checkbox jquery-checkbox-checked">
  <span class="mark jquery-checkbox-disabled">
    <img src="empty.png">
  </span>
</span>

As you can see, The checkbox does get disabled, but the elements that provide you that custom look are just regular spans which are still clickable.

It appears that Dmitry is passing on the click events of these spans to the hidden checkbox.

a.wrapper.click(function(e) {
                b.trigger('click', [e]);
                i(e);
                return false
            });

This is why you're still getting the click events of the checkbox even though it is disabled. The checkbox is being programatically clicked.

So if your issue is that you're trying to detect when the checkbox has changed, then I recommend you use the change event as it does not seem to suffer from the same issue.

http://jsfiddle.net/vW6gj/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文