jQuery droppable元素id未定义

时间:2014-12-08 21:43:58

标签: javascript php jquery ajax

我正在尝试从droppable函数中获取li元素id,但它只是返回undefined,任何想法为什么会出现这种情况?

每个li元素如下:

 <li class="ui-state-default" data= "<?php $row['ID']?>">
 <?php echo $row['name']?>    </li>
一旦失败,我就有了这个:

$(document).ready(function(){   

$( "#draggable" ).draggable();
$( "#droppable" ).droppable({

drop: function(event, ui) {



var ID = $(this).data("ID");
var ajaxurl = 'delete.php';

    $.post(ajaxurl,{ID:ID});
        alert("You have deleted the row successfully" + ID);


$(ui.draggable).remove();}

 });
});
警告中的

,ID被打印出来只是“未定义”。知道应该如何写这个吗?

2 个答案:

答案 0 :(得分:2)

您需要将ID放在data-ID属性中才能使用.data("ID")访问它。

<li class="ui-state-default" data-ID="<?php $row['ID']?>">

答案 1 :(得分:1)

查看此代码。

$(document).ready(function(){
    $( "#draggable" ).draggable();
    $( "#droppable" ).droppable({
        drop: function(event, ui) {
            var ID = $(this).data("ID");
            var ajaxurl = 'delete.php';
            $.post(ajaxurl,{ID:ID}, function(ID){
                alert("You have deleted the row successfully" + ID);
            });
            $(ui.draggable).remove();
    });
});

ID参数未定义,因为不在范围内,您需要输入ID数据参数