jQueryUI,单选按钮状态和单击事件 [英] jQueryUI, radio button state, and click events

查看:122
本文介绍了jQueryUI,单选按钮状态和单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中包含多组用于设置选项的单选按钮.当一个单击特定事件时,默认情况下使用单击事件处理程序选择其他事件.该功能可以完美运行,但是按钮的视觉状态存在问题.

I have a page with several sets of radio buttons that are used to set options. When one clicks on specific ones, others are selected by default using click event handlers. The functionality works perfectly, but there is an issue with the button's visual state.

我正在使用jQueryUI的.buttonset()方法来改善美观性,并且当我以编程方式触发.click()事件时,按钮不会在视觉上改变状态.这可能导致当前选项与屏幕上显示的选项大不相同.

I'm using jQueryUI's .buttonset() method to improve the aesthetics, and when I trigger a .click() event programatically, the button does not change state visually. This can result in the current options being quite different from what appears on screen.

示例代码说明了问题:

<fieldset>
    <label for="button1">Button 1</label>
    <input type="radio" id="button1" name="test" />

    <label for="button2">Button 2</label>
    <input type="radio" id="button2" name="test" />
</fieldset>

$('fieldset').buttonset();

$('#button2').click(function() {
    alert('button 2 clicked');
});

$('#button2').click();

我还设置了一个小提琴,因此您可以根据需要查看它: http://jsfiddle .net/T5MGh/

​I also set up a fiddle so you can see it in action, if you so desire: http://jsfiddle.net/T5MGh/

正如您所期望的,警告框会在页面加载时按预期方式弹出,但是该按钮不会像用户单击一样在视觉上发生变化.

As you would expect, the alert box pops up on page load as it should, but the button does not change visually as it does from a user-click.

有什么想法吗?

推荐答案

您可以单击按钮集使用的实际标签,如下所示:

You can click the actual label that the button set uses, like this:

$('[for=button2]').click();

之所以起作用,是因为您的结构在.buttonset()之后看起来像这样:

This works because your structure looks like this after .buttonset():

<fieldset class="ui-buttonset">
    <label for="button1" aria-pressed="false" class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-left" role="button" aria-disabled="false"><span class="ui-button-text">Button 1</span></label>
    <input type="radio" id="button1" name="test" class="ui-helper-hidden-accessible">

    <label for="button2" aria-pressed="true" class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-right ui-state-active ui-state-hover" role="button" aria-disabled="false"><span class="ui-button-text">Button 2</span></label>
    <input type="radio" id="button2" name="test" class="ui-helper-hidden-accessible">
</fieldset>

由于jQuery UI的工作方式,它最初不起作用,它依赖通过<label>进入click,并且实际上是从浏览器中单击<input>那个.

It doesn't work initially because of how jQuery UI does it, it relies on the click coming through the <label>, and the defult browser behavior actually clicking the <input> from that.

这篇关于jQueryUI,单选按钮状态和单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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