如何延迟 .live() 直到 UI 更新?
我正在使用 jQuery 和 jQuery UI,我试图通过添加 jQuery UI 按钮在 false 和 true 之间切换的“aria-pressed”属性来读取复选框状态。
$('.slideButton').live('click', function() {
alert($(this).attr('aria-pressed'));
});
然而,此代码似乎在 jQuery UI 更新 aria-pressed 之前读取了 aria-pressed。所以我的价值观不可靠。我可以安排它在 UI 更新后执行还是可以让它等待?
I am using jQuery and jQuery UI and i am trying to read a checkbox state through the addition of the "aria-pressed" property that jQuery UI Button toggle between false and true.
$('.slideButton').live('click', function() {
alert($(this).attr('aria-pressed'));
});
This code however seems to read the aria-pressed before jQuery UI has updated aria-pressed. So i unreliable values. Can i schedule it to execute after the UI has updated or can i make it wait?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能将侦听器添加到标签或跨度后面的实际复选框吗?
这应该有效,因为一旦您单击标签,您就会更改复选框中的值。
好吧,我已经为您编写了一个现场示例,演示了其工作原理的总体思路,并且如何检索复选框的状态。
在您的情况下,由于复选框是由 JQuery 动态添加的,您必须使用实时事件,但它基本上是相同的事情
这是一个示例,其中包含一些额外的脚本和检查,主要用于演示目的。
Can't you add a listener to the actual checkbox behind the label or span?
This should work since, once you click the label, you change the value in the checkbox.
Alright, I've composed a live example for you that demonstrates the general idea of how it works, and how you retrieve the status of the checkbox.
In your case, since the checkbox is added dynamically by JQuery you have to use the live event, but it is basically the same thing
Here's an example with some additional scripting and checking, mostly for demonstrative purposes.
您可以在切换完成后使用回调执行
alert($(this).attr('aria-pressed'));
。我不太确定您是否使用
.toggle()
但您需要使用回调来确保顺序执行。You can use a callback to execute the
alert($(this).attr('aria-pressed'));
after the toggle has completed.I'm not too sure if your using
.toggle()
or not but you would need to use a callback either way to ensure sequential execution.