获取draggable元素的值

时间:2011-12-09 11:41:57

标签: jquery

我的代码如下:

 $(function() {
    $("#products li").draggable({
        cursor:'    move',
        opacity:    0.3,
        revert:     true,
        proxy:      'clone',
    });
    $("tr").droppable({
        drop:function(e,source){
            alert (source);
            var name = $("#products li").attr("id");
            alert (name);  
            var pid = $(this.draggable).attr("id") ;
            var add = $(this).text() ;
            var sum = pid+"-"+add;
            alert(sum);
             $.ajax({
                type:       "POST",
                url:        "add-shipment.php",
                data:       "address="+sum,
                success:    alert(sum+"has addes into database")
                });
        },
        out:function(){

        }
    });
});

问题是列表是由PHP动态生成的。它只给出第一个元素的id

1 个答案:

答案 0 :(得分:1)

正在拖动的元素将作为source变量传递给您的函数。

因此,要获取示例中的值,请使用:

var pid = source.draggable.id;
var name = source.draggable.attr("name");
var add = source.draggable.text() 

完整示例:

$(function() {
    $("#products li").draggable({
        cursor:'    move',
        opacity:    0.3,
        revert:     true,
        proxy:      'clone',
    });

    $("tr").droppable({
        drop: function(e, source) {
            var $el = source.draggable;

            var name = $el.attr("id");
            var pid = $el.attr("id");
            var add = $el.text();
            var sum = pid + "-" + add;

            alert(sum);

            $.ajax({
                type:       "POST",
                url:        "add-shipment.php",
                data:       "address="+sum,
                success:    alert(sum+"has addes into database")
            });
        },
        out: function(){

        }
    });
});