jQuery Selectable - 多个List的一个函数

时间:2015-04-08 15:02:21

标签: javascript jquery selectable jquery-ui-selectable

我有来自jQuery类型“selectable”的两个(或更多)有序列表,包含多个项目,如下所示:

<ol id="selectable" class="0">
   <li class="ui-widget-content">
      <div>
        <img src="..."></br>
        <span>Item 1 - Row 1</span>
      </div>
   </li>
   <li class="ui-widget-content">
      <div>
        <img src="..."></br>
        <span>Item 2 - Row 1</span>
      </div>
   </li>
</ol>
<ol id="selectable" class="1">
   <li class="ui-widget-content">
      <div>
        <img src="..."></br>
        <span>Item 1 - Row 2</span>
      </div>
   </li>
   ...
</ol>

现在如果有两个函数来查询所选“可选”的索引

$( "#selectable.0" ).selectable({
filter: "li",
selecting: function (event, ui) {
  $(event.target).children('.ui-selecting').not(':first').removeClass('ui-selecting');
  row = event.target.className[0]},
  stop: function() {
    var result = $( "#select-result" ).empty();
    $( ".ui-selected", this ).each(function() {
      index = $( "#selectable.0 li" ).index( this );
      values[row] = String(index)
    });
  }
});

$( "#selectable.1" ).selectable({
filter: "li",
selecting: function (event, ui) {
  $(event.target).children('.ui-selecting').not(':first').removeClass('ui-selecting');
  row = event.target.className[0]},
  stop: function() {
    var result = $( "#select-result" ).empty();
    $( ".ui-selected", this ).each(function() {
      index = $( "#selectable.1 li" ).index( this );
      values[row] = String(index)

    });
  }
});

这段代码非常冗余,我希望将这两个函数合并为一个,它可以决定选择哪个列表,还可以返回所选项目的索引。重要的是,我不希望所有项目的索引

0 个答案:

没有答案