如何使用 jQuery UI 以编程方式选择可选择项?
我有一系列可供选择的项目。我想在某处添加一个按钮来激活其中的预设选择。我有办法做到这一点吗?
我想要的是告诉它“选择这些人”,然后让所有事件正常触发,这样我就不必手动调用所有这些选择事件。
- 选择
- 选择
- 开始
- 停止
- 取消选择
- 取消选择
而且,我认为可能有选择事物时也会设置/清除的数据。所以不仅仅是添加那些css类。
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.
More info: The events that I talk about are the ones listed in their api and on their demo page:
- selected
- selecting
- start
- stop
- unselected
- unselecting
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以下是 Alex R 处理多个元素的代码的变体
http://jsfiddle.net/XYJEN/1/
更新:
jQueryUI 1.10,根据 kmk 的评论:http://jsfiddle.net/XYJEN/163/
Here is a variation of Alex R's code working with multiple elements
http://jsfiddle.net/XYJEN/1/
Update:
jQueryUI 1.10, per comments from kmk: http://jsfiddle.net/XYJEN/163/
假设 jQuery UI 网站上有可选示例 (http://jqueryui.com/demos/selectable/ ):
你可以有一个像这样的函数:
并像这样使用它:
这可以改进以允许选择元素集合,但它是让你继续前进的起点。
Assuming the selectable sample on the jQuery UI website (http://jqueryui.com/demos/selectable/):
you can have a function like:
and use it like:
This can be improved to allow selecting a collection of elements, but it's a starting point to get you going.
你开始:
在
selectable.js
中的_mouseStop
之后添加这个,然后你可以说:...或无论你喜欢什么。
玩得开心! :)
There you go:
Add this after
_mouseStop
inselectable.js
and then you can say:... or whatever you like.
Have fun! :)
编辑:抱歉造成误解,我正在编辑我的答案。
所以,是的,对象的选择可能对应于 ui-selected 类,所以你可以做的是:
Edit : Sorry for the misunderstanding, I'm editing my answer.
So, yes it is possible the selection of the object corresponds to the class ui-selected, so what you can do is :
是否无法使用 .trigger('selected') 手动触发
selected
事件?Is it not possible to trigger the
selected
event manually with .trigger('selected')?使用 Ionut 代码,怎么样:
?
Using Ionut code, how about:
?