jQuery / javascript不同的行为与用户单击和编程单击复选框 [英] jQuery/javascript different behaviours with user click and programatically click a checkbox

查看:132
本文介绍了jQuery / javascript不同的行为与用户单击和编程单击复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点难以解释,因此我在这里设置了一个jsFiddle。

A bit hard to explain, so I've set up a jsFiddle here.

基本上,当用户单击复选框时,我会触发一些行为。在另一个地方,我试图编程地点击复选框,我需要看到完全相同的行为。它不工作,看起来与点击的顺序和确定checked属性有关。

Basically, I have some behaviour triggered when a user clicks a checkbox. In another place I'm trying to programatically click the check box and I need to see the exact same behaviour. It doesn't work, seemingly something to do with the order of the click and determining the checked attribute.

如何获得我没有求助的行为到一个hack?

How can I get the behaviour I'm looking for without resorting to a hack?

推荐答案

我倾向于使用 change (因为jQuery规范了行为,它不需要 blur ),然后触发这个事件:

I tend to use change handler here instead (since jQuery normalizes the behavior, it doesn't require a blur), then fire that event like this:

$('span').click(function() { $('.test').click().change(); });

然后,你明确的关于订单...和你没有原生操作vs事件处理程序顺序,您可以在此处测试

Then, you're explicit about the order...and you don't have the issue of the native action vs event handler order, you can test it out here.

这篇关于jQuery / javascript不同的行为与用户单击和编程单击复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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