dreamy1992 2014-12-03 05:24
浏览 81
已采纳

jQuery更改ID <than $(this).attr(“id”)的所有DIV的ID;

I have an sortable grid of elements, that updates its value to DB after user moves the element. The problem is, I don't know how to update the id of all previous elements to a new one without reloading the page.

In PHP, I would do something like this (code from FAQ of one of my older pages), but I can't use PHP (it has to happen without any page reloading, right after user places element he moved):

if ($old_order > $order){
    $result = dbquery("UPDATE faq SET orders=orders+1 WHERE orders>='$order' AND orders<='$old_order'");
}else{
    $result = dbquery("UPDATE faq SET orders=orders-1 WHERE orders>='$old_order' AND orders<='$order'");
}

I would like to do that with jQuery, basically I have 7 elements with id from 0 to 6 and every time user changes the position, I serialize it and send it with ajax to an php code that saves it.

What it does now:

  1. Move elemtent 1 to position 4.
  2. Saves and works.
  3. Move element 3 to position 2
  4. Moves element 1 from position 4 back to his old one, as the ID of it is still 1 and not 4.

What I want to do:

  1. Move element 1 to position 4
  2. Change ID of element 1 from 1 to 4
  3. Change ID of element 2, 3 and 4 by -1 to id 1, 2 and 3

I hope somebody understands me and can help me.

jQuery code I actauly use:

$(".content-page").sortable({
    start: function(e,ui){
        ui.placeholder.height($(".sorted-icons").outerHeight(true));
        ui.placeholder.width($(".sorted-icons").outerWidth(true));
    },
    placeholder: 'placeholder',
    items: '.sorted-icons:not(.new_icon)',
    update: function(e,ui) {
        var order = $(this).sortable("serialize") + '&order=icons' + '&content_id=' + $(this).attr("data-shortcut-id");
        console.log(order);
        $.post("page_ajax.php", order).done(function(data){
            console.log(data);
        }).fail(function(data){
            console.log(data);
        });
    }
}).disableSelection();

Html code basically looks like this with content inside of that div thats irelevant:

echo "<div class='sorted-icons' id='icon_$id'>";

If you have any questions, just comment and Ill try to answer them and update the queston.

Fixed jQuery:

var i = 0;
$(this).children('.sorted-icons').each(function(){
    $(this).attr('id', 'icon_' + i);
    i++;
});

Still have problem with PHP part tho. Its saving them in weird orders.

  • 写回答

1条回答 默认 最新

  • dongmin4052 2014-12-03 07:01
    关注

    Ok, so I had one little bug in my PHP code, but I managed to fix the jQuery with really simple pice of code:

    $(".content-page").sortable({
        start: function(e,ui){
            ui.placeholder.height($(".sorted-icons").outerHeight(true));
            ui.placeholder.width($(".sorted-icons").outerWidth(true));
        },
        placeholder: 'placeholder',
        items: '.sorted-icons:not(.new_icon)',
        update: function(e,ui) {
            var order = $(this).sortable("serialize") + '&order=icons' + '&content_id=' + $(this).attr("data-shortcut-id");
            console.log(order);
            $.post("page_ajax.php", order);
            // THIS PART //
            var i = 0;
            $(this).children('.sorted-icons').each(function(){
                $(this).attr('id', 'icon_' + i);
                i++;
            });
            // THIS PART //
        }
    }).disableSelection();
    

    Hopefully might help somebody else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料