【问题标题】:jquery sortable with item restriction带有项目限制的jquery可排序
【发布时间】:2018-06-29 12:07:21
【问题描述】:

我想避免第 7 项被拖入红色列表。

$("span").sortable({
  connectWith: ".con"
}).disableSelection();
#red {
  margin-top: 10px;
  border: 1px solid red;
  display: block;
}

#green {
  margin-top: 10px;
  border: 1px solid green;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.8.9/jquery-ui.min.js"></script>

<form id="form">
  <span id="red" class="con">
        <div>Item 1</div>
        <div>Item 2</div>
        <div>Item 3</div>
        <div>Item 4</div>
        <div>Item 5</div>
    </span>
  <span id="green" class="con">
        <div>Item 6</div>
        <div>Item 7</div>
    </span>
</form>

我想我必须使用停止事件并检查项目 7 是否在红色列表中,如果是则将项目返回到原始位置。

【问题讨论】:

  • 您只针对 7 吗?或 6 和 7
  • 我的目标只有 7 个

标签: jquery jquery-ui jquery-ui-sortable


【解决方案1】:

您可以使用 sortable('stop') 来使用“取消/停止”功能:

$("span").sortable({
  remove: function(e, ui) {
    if(ui.item.hasClass("id_7")) {
        alert('id 7');
        e.preventDefault();
    }
  },
  connectWith: ".con"
});

【讨论】:

  • 第7项在绿色列表中必须是可排序的,但在红色列表中不能拖动
  • 如果你在代码中将 'stop' 替换为 'remove' 就可以了
  • 弗兰克,是的,你是对的,将 stop 替换为 remove 也可以;-)
  • 用你现在的代码不可能将一个项目拖到绿色列表中...所以用删除替换停止,我会给你答案...你给了我很好的提示
猜你喜欢
  • 2019-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-28
  • 2020-05-15
  • 1970-01-01
  • 2013-07-02
  • 2017-10-22
相关资源
最近更新 更多