简体   繁体   English

如何使用jQuery UI(可拆分)交换ID?

[英]How do I swap ID's using jQuery UI (Sortable)?

From what I understand, when you use jQuery Sortable the actual list items ( li ) are rearranged in the HTML code. 据我了解,当您使用jQuery Sortable时,实际的列表项( li )将在HTML代码中重新排列。 What I'd like to do is create the illusion of sorting but just move the image names around (perhaps this might take swapping ID's). 我想做的是创建排序的错觉,但只需移动图像名称即可(也许这可能需要交换ID)。 I've setup a Fiddle to illustrate my example (in this example, the list items are sortable but the ID's and selected items go with it, I want these to keep their order): 我设置了一个小提琴来说明我的示例(在此示例中,列表项是可排序的,但ID和选定项随其排序,我希望它们保持顺序):

JS Fiddle: http://jsfiddle.net/7QFB7/ JS小提琴: http : //jsfiddle.net/7QFB7/

What is the best way to keep everything in the same order, minus the image names? 减去图像名称,使所有内容保持相同顺序的最佳方法是什么? I need the ID's of the list items and select boxes changed as well as the names of the inputs so that even if they are rearranged the only items that will technically move are the image names. 我需要更改列表项和选择框的ID,以及输入的名称,以便即使重新排列了输入,从技术上讲,唯一会移动的项就是图像名称。 So the end result could look something like this: 因此最终结果可能如下所示:

    <li id="thumb-1"><select id="image1"></select><input name="image1_path" value="imagename3.jpg"></li>

Notice that in the above line of code all the names and id's have a 1, but the image name has the 3. This is what I'm trying to achieve. 请注意,在上面的代码行中,所有名称和ID均具有1,而图像名称具有3。这就是我要实现的目标。

Thank you. 谢谢。

Could be done like this: 可以这样完成:

DEMO DEMO

stop: function (event, ui) {
        ui.item.removeClass('dragging');
        $(this).find('li').each(function(i){
            var index = i+1,
                select =  $(this).find('select')[0];
           select.id = "image"+index;
            select.value = index;
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM