我使用jQuery Selectable(http://api.jqueryui.com/selectable/)并且需要知道操作完成后所选元素的数量。
//Example Code
$('body').selectable({
selected: function(event,ui) {
var $selectedItems = ui.selected;
alert($selectedItems.length); // This is always 1
}
});
变量" selectedItems"返回选中的元素。我可以像这样循环遍历:
$selectedItems.each( function() {
// do something
});
但我只是想知道选择了多少元素。
"长度"属性总是返回1.在上面的循环中放置一个计数器似乎不对。必须有某种方法这样做吗?
感谢您的帮助。
答案 0 :(得分:3)
如果您想在选择后运行一个回调并检查有多少选项,则可以使用Graph<V, E> newGraph = ...; // create the appropriate graph type
for (V v : Sets.intersection(g1.getVertices(), g2.getVertices()) {
newGraph.addVertex(v);
}
for (E e : Sets.intersection(g1.getEdges(), g2.getEdges()) {
// Assume that each each e connects the same set of vertices in both
// g1 and g2. Otherwise you need to check that as well.
V v1 = g1.getEndpoints(e).getFirst();
V v2 = g1.getEndpoints(e).getSecond();
// This contains check is necessary because the default implementations
// of addEdge() will add v1 and v2 if they are not already present in
// the graph, which you don't want to do if they're not in the intersection.
if (newGraph.containsVertex(v1) && newGraph.containsVertex(v2)) {
newGraph.addEdge(e, v1, v2);
}
}
事件。选择完成后运行一次。然后,您可以按添加的班级检查所选项目的数量,即stop
。
ui-selected
&#13;
$( "#selectable" ).selectable({
stop: function(event,ui) {
var selectedItems = $('.ui-selected', this);
console.log(selectedItems.length);
}
});
&#13;
#selectable .ui-selecting {
background: #ccc;
}
#selectable .ui-selected {
background: #999;
}
&#13;