获取值数据<td>javascript

Get value data <td> javascript

本文关键字:td javascript 数据 获取      更新时间:2023-09-26

如何获得数据拖拽时的td值?我需要帮助,谢谢。

这里是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>
我不知道我的错在哪里。请给我一个答案。

代替此处

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

stop:事件侦听器中,尝试以下操作:

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

Replace

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

var val1 = $("td").val();
http://api.jquery.com/val/