duankui1532 2014-02-09 21:25
浏览 42
已采纳

关于拖动更新的jquery,更改db

I have a set of Data (posted below) that gets sorted by 'section'. When it is loaded on the page each item gets sorted into a UL according to its section.

What I am trying to accomplish is when the user drags an item to another section and drops it, the section in the DB will automatically update (ie. drag item2 to section 2, section will update from 0 to 2).

I dont care about keeping the items in a specific order, the only thing that needs to update is the section #

Functionality Demonstrated with this FIDDLE

Database

id   name    section
 1   item1      0
 2   item2      0
 3   item3      0
 4   item4      0

HTML

 <!-- Items are dynamically inserted into ULs based on section #, unique ids become li id -->
    <!-- DATA = 0 -->
    <ul id="main" class="sortable" data="0">
        <li id="1">Item 1</li>
        <li id="2">Item 2</li>
        <li id="3">Item 3</li>
        <li id="4">Item 4</li>
    </ul>
    <!-- DATA = 1 -->
    <ul id="box1" class="sortable" style="background-color: blue" data="1">

    </ul>
    <!-- DATA = 2 -->
    <ul id="box2" class="sortable" style="background-color: green" data="2">

    </ul>

jQuery

after a drag event has completed im trying to call a function that runs a php script to update the DB but im having an issue with passing the ID variable through

$('.sortable').sortable({
    connectWith: '.sortable',
    update: function(event, ui) {
        //Get id of element moved and the id of ul it was dropped in 
        updateDB(updateid, sectionid);
    }
});

function updateDB(updateid, sectionid) {
    $.ajax({
       //grab script, pass id and update database 
    });
}

Can someone point me in the right direction on how to grab the data so I can update the db properly

  • 写回答

2条回答 默认 最新

  • douju5062 2014-02-09 21:45
    关注

    I hope I understand what you asked, you want to get the element id which was dragged to some place? If so, here is the code:

    $('.sortable').sortable({
        connectWith: '.sortable',
        update: function(event, ui) 
        {
            alert(ui.item.attr('id'));
            var id = ui.item.attr('id');
            updateDB(id);
        }
    });
    
    function updateDB(id) {
        $.ajax({
           //grab script, pass id and update database 
        });
    }
    

    for finding the box in which the element was dragged to, you can use: alert(ui.item.parent().attr('id'));

    Note: the ui object has in it an attribute called item which holds the jquery element which was dragged. here is the link to the API. By having this object you can access all it attributes with the .attr('attribute') function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮