2015-10-05 78 views
-3

如何在拖动数据时获取td值?谢谢你,我需要帮助。
获取数值数据​​javascript

这里的JavaScript:

<script> 
$(function() { 
$("#sortable1, #sortable2").sortable({ 
connectWith: ".connected" 
}); 
$("#sortable1, #sortable2").disableSelection(); 
$("#sortable1, #sortable2").sortable({ //here i want to alert the value when stop dragging 
stop: function(event, ui) { 
var val1 = $("td").innerHTML; 
alert(val1); 
} 
}); 
}); 
</script> 

这里html代码:

<?php if(!empty($query)){?> 
<table id="todo" name="todo" class="table table-bordered" style='width:150px'><thead> 
<tr> 
<th style="text-align:center;background-color:#eee">TO DO</th> 
</tr> 
</thead>    
<tbody id="sortable1" class="connected"> 
<?php foreach($query as $row):?> 
<tr> 
<td style="width:150px;background-color:#eee" id="<?php echo $row->list;?>" name="<?php echo $row->list;?>" value="<?php echo $row->list;?>"><?php echo $row->list;?></td> 
</tr> 
<?php endforeach;}?> 
</tbody> 
</table> 
<table id="today" name="today" class="table table-bordered" style='width:150px'> 
<thead> 
<tr> 
<th class="ui-state-highlight" style="text-align:center">TODAY</th> 
</tr> 
</thead> 
<tbody id="sortable2" class="connected"> 
</tbody> 
</table> 

我不知道我的错。请给我一个答案。

回答

-1

更换

var val1 = $("td").innerHTML; 

随着

var val1 = $("td").val(); 

http://api.jquery.com/val/

+0

val()for td ... sure ?? –

0

更换这里

var val1 = $("td").first().html();//first() to ensure only 
+0

但它也获得第一价值,那么如何获得另一个价值?不仅仅是第一个TD,谢谢 –

+0

,正如我在你的例子中看到的那样,如果你调用所有td的html,它将会产生多重性 –

+0

不是所有的td,我的意思是在拖动时调用TD,只是把价值拖到TD TD –

0

stop:事件侦听器,尝试以下东西:

stop: function(event, ui) { 
    var val1 = $(ui.item).html(); //ui.item is a jQuery object representing the current dragged element 
    alert(val1); 
} 
+0

谢谢,我找到了答案: 'start:function(event,ui){val1 = $(ui.item).find('td')。attr('id'); }, stop:function(event,ui){alert(document.getElementById(val1).innerHTML); }' –

+0

确实需要将代码添加到'start:'处理程序中吗?我认为'stop:'就足够了''(ui.item)''也可以在'stop:'中使用。 – vijayP

+0

是的,在开始我把ID,然后停止我把该ID从innerHTML –