jQuery 使用可排序和动态添加元素(实时刷新)
我有一个
<form id="form">
<span class="con">
<div class="ui-state-highlight">Item 1</div>
<div class="ui-state-highlight">Item 2</div>
...
</span>
</form>
我正在使用 sortable 函数使 div 可排序。
$("span").sortable({
connectWith: ".con"
}).disableSelection();
我正在动态添加里面的div。但可排序无法识别新添加的跨度。我知道有一个可排序的 refresh
选项,它应该像 live()
一样工作并重新识别新添加的内容,但我不知道如何将它与此一起使用例子。
检查http://jsfiddle.net/mRyVp/8/。单击该按钮可添加更多带有 div 的 Span。您将看到可以对最初在 DOM 中的 div 进行排序,但不能对新添加的 div 进行排序。
I have a <form id="#form">
that have a <span class="con">
and inside the span i have many divs that needs to be sortable.
<form id="form">
<span class="con">
<div class="ui-state-highlight">Item 1</div>
<div class="ui-state-highlight">Item 2</div>
...
</span>
</form>
I'm using the sortable function to make div Sortable.
$("span").sortable({
connectWith: ".con"
}).disableSelection();
I'm dynamically adding with divs inside. But sortable is not recognizing newly added spans. I know there's a refresh
option for sortable that is supposed to work like live()
and reognize newly added content, but i don't see how i can use it with this example.
Check http://jsfiddle.net/mRyVp/8/. Click on the button to add more spans with divs inside. You will see that you can sort div that were initially in DOM but not newly added ones.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来你有
class="connectedSortable"
和
connectWith: ".con"
在将
con
类添加到原始 div 中就可以了。在此处更新。It seems that you have
class="connectedSortable"
inAnd
connectWith: ".con"
inAdding
con
class to original div will just be fine. Update here.试试这个:
当您单击“添加另一个选项”时,脚本会将新的可排序“项目”添加到列表中
Try this:
When you click over 'add another option', the script will add new sortable 'Item' into the list
如下所示更改按钮单击事件,并且它有效。不需要进行其他更改。
我在 jfiddler 中尝试过,它成功了。新添加的项目成为列表的一部分并且也可以排序。
我将 x 定义为元素,并进一步将 x 附加到 #form 内的“.con”类。
此更新示例的屏幕截图如下所示:
Change Button Click event as given below, and it works. No other changes are required.
I tried in jfiddler and it worked. Newly added items becomes part of list and are sortable as well.
I defined x as element and further x is appenedTo ".con" class inside #form.
The screen-shot of this updated example is shown below: