jQueryUI、单选按钮状态和单击事件

发布于 2024-09-06 06:37:52 字数 817 浏览 0 评论 0原文

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

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

说明问题的示例代码:

<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 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.

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.

Sample code to illustrate the problem:

<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();

​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.

Any thoughts?

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

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

发布评论

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

评论(2

情话已封尘 2024-09-13 06:37:53

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

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

这之所以有效,是因为您的结构在 .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 的工作方式,它最初不起作用,它依赖通过进行点击,默认浏览器行为实际上点击< /code> 从此。

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

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

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>

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.

风吹过旳痕迹 2024-09-13 06:37:53

在 jQuery / jQuery UI 的最新版本中,行为可能会发生变化,将作为此问题发布的原始代码的行为呈现为此问题的作者想要的内容(单击代码中的按钮可以同时调用事件并在视觉上改变按钮)。

您也可以在引用的 jsfiddle 中看到这一点。所以看来这个答案只与旧版本的 jQuery / jQuery UI 相关。

It may appear that in more recent versions of jQuery / jQuery UI, the behavior has changed, rendering the behavior of the original code posted as this question to what the author of this question wanted (clicking the button from code takes care of both invoking the event and visually changing the button).

You can see that in the referenced jsfiddle as well. So it seems this answer is only relevant for older versions of jQuery / jQuery UI.

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