【问题标题】:jquery sortable alert this list idjquery 可排序警报此列表 ID
【发布时间】:2011-05-05 14:26:56
【问题描述】:

真的需要帮助

我正在使用可排序的 jquery。

我希望只从被拖动的列表元素中获取 id。

“并非全部关闭”

这是一个例子http://jsfiddle.net/isimpledesign/85LdV/1/

这会提醒一个数组,但我需要它来只返回被拖动元素的 id,以便我可以将它传递给 php 文件。

有人可以帮我吗????

【问题讨论】:

    标签: jquery user-interface jquery-ui-sortable


    【解决方案1】:

    只是为了澄清一下乍得的回答 -

    $(function() {
        $("#sortable").sortable({
            update: function(event, ui) {
                // i need to get the class text that is being dragged i.e
                var order = $(this).sortable("serialize");
                alert(order); 
                /*
                 No need to bind any other events, ui.item is the dragged
                 item in 'update' too and we only want to capture the id when the sort
                 has changed presumably
                */
                alert(ui.item.attr('id'));
                /*
                 No need for subscripting, ui.item is a jquery object so 
                 we can just call attr() on it to get the ID
                */
            }
        });
    });
    

    【讨论】:

    • +1 哇,我什至都懒得检查更新事件的 ui 参数,这太愚蠢了。好电话。
    • 别担心,现在还早:)
    【解决方案2】:

    使用start 事件:

    $(function() {
       $("#sortable").sortable({
          update: function(event, ui) {
             // i need to get the class text that is being dragged i.e
            var order = $(this).sortable("serialize");
            alert(order);   
          },
          //Start event fires on the start of a sort
          //you can store the id from in here
          start: function(event, ui) {
             //here ui.item contains a jquery object that is the item being dragged
             alert(ui.item[0].id);
          }
       });
    });
    

    【讨论】:

    • @Luccas 老实说,我不确定您的意思,它们是在不同时间触发的不同事件,传递了不同的信息。 jQuery.sortable
    【解决方案3】:

    使用这个:

    $(function() {
        var lastMoved = null;
        $("#sortable li").mousedown(function(){
            lastMoved = this;
        });
        $("#sortable").sortable({
          update: function(event, ui) {
            alert($(lastMoved).attr("id"));
          }
        });
    });
    

    它已经过测试并且可以正常工作。 希望这可以帮助。干杯。

    【讨论】:

    • 这是不可靠的,因为它不再依赖排序了。即使排序没有改变,只要单击对象,您就会返回 ID。
    • @HurnsMobile。不你错了。它显示了 update 方法中的值,这仅在您更改订单时发生。请对其进行分析并重新考虑您的反对意见。
    猜你喜欢
    • 2019-03-10
    • 2011-01-25
    • 2013-12-20
    • 1970-01-01
    • 2011-04-02
    • 2018-09-11
    • 2010-10-28
    • 2014-01-01
    • 2012-11-24
    相关资源
    最近更新 更多