如何通过jQuery UI以编程方式选择选择项? [英] How to programmatically select selectables with jQuery UI?

查看:71
本文介绍了如何通过jQuery UI以编程方式选择选择项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列可供选择的项目.我想在某个位置添加一个按钮,以激活其中的一个预设选择.有办法吗?

I have a range of items that are selectable. I would like to add a button somewhere that activates a preset selection amongst those. Is there a way I can do that?

我想告诉它选择这些人",然后按正常方式触发所有事件和所有事件,因此我不必手动调用所有这些选择事件.

What I would like is to tell it to "Select these guys" and then have all the events and all fired as normal, so I don't have to call all of those selection events manually.

更多信息:我谈论的事件是其事件中列出的事件api 其演示页面上:

More info: The events that I talk about are the ones listed in their api and on their demo page:

  • 已选择
  • 选择
  • 开始
  • 停止
  • 未选中
  • 取消选择

而且,我认为在选择内容时可能还会设置/清除数据.因此,不仅仅是添加这些CSS类.

And also, I think there might be data that is set/cleared as well when selecting things. So it's not just to add those css classes.

推荐答案

这是Alex R的代码的变体,可以处理多个元素

Here is a variation of Alex R's code working with multiple elements

http://jsfiddle.net/XYJEN/1/

function SelectSelectableElements (selectableContainer, elementsToSelect)
{
    // add unselecting class to all elements in the styleboard canvas except the ones to select
    $(".ui-selected", selectableContainer).not(elementsToSelect).removeClass("ui-selected").addClass("ui-unselecting");

    // add ui-selecting class to the elements to select
    $(elementsToSelect).not(".ui-selected").addClass("ui-selecting");

    // trigger the mouse stop event (this will select all .ui-selecting elements, and deselect all .ui-unselecting elements)
    selectableContainer.data("selectable")._mouseStop(null);
}

更新:

jQueryUI 1.10,来自kmk的注释: http://jsfiddle.net/XYJEN/163/

jQueryUI 1.10, per comments from kmk: http://jsfiddle.net/XYJEN/163/

这篇关于如何通过jQuery UI以编程方式选择选择项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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