jQuery 使用可排序和动态添加元素(实时刷新)

发布于 2024-10-26 23:00:01 字数 812 浏览 7 评论 0原文

我有一个

,它有一个 ,在跨度内我有很多 div需要可排序。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

芸娘子的小脾气 2024-11-02 23:00:01

看来你有 class="connectedSortable"

<span class="connectedSortable">
    <div class="ui-state-highlight">Item 1</div>
    <div class="ui-state-highlight">Item 2</div>
    ... 
</span>

connectWith: ".con"

$("span").sortable({
    connectWith: ".con"
}).disableSelection();

con 类添加到原始 div 中就可以了。在此处更新

It seems that you have class="connectedSortable" in

<span class="connectedSortable">
    <div class="ui-state-highlight">Item 1</div>
    <div class="ui-state-highlight">Item 2</div>
    ... 
</span>

And connectWith: ".con" in

$("span").sortable({
    connectWith: ".con"
}).disableSelection();

Adding con class to original div will just be fine. Update here.

晚雾 2024-11-02 23:00:01

试试这个:

$('button').click(function() {
    var x = $('<div class="ui-state-highlight">Item '+($('.con div').length+1)+'</div>');
    x.appendTo('#form .con')
});

$("span").sortable({
    connectWith: ".con"
}).disableSelection();

当您单击“添加另一个选项”时,脚本会将新的可排序“项目”添加到列表中

Try this:

$('button').click(function() {
    var x = $('<div class="ui-state-highlight">Item '+($('.con div').length+1)+'</div>');
    x.appendTo('#form .con')
});

$("span").sortable({
    connectWith: ".con"
}).disableSelection();

When you click over 'add another option', the script will add new sortable 'Item' into the list

浮生未歇 2024-11-02 23:00:01

如下所示更改按钮单击事件,并且它有效。不需要进行其他更改。
我在 jfiddler 中尝试过,它成功了。新添加的项目成为列表的一部分并且也可以排序。

$('button').click(function() {
    var x = $('<div>aaaaaaaaa</div>');
    x.appendTo('#form .con')
});

我将 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.

$('button').click(function() {
    var x = $('<div>aaaaaaaaa</div>');
    x.appendTo('#form .con')
});

I defined x as element and further x is appenedTo ".con" class inside #form.

The screen-shot of this updated example is shown below:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文