【发布时间】:2021-01-08 20:36:34
【问题描述】:
如何使用 JavaScript 获取 ID 号(数据 ID)。我正在使用 sortable.min.js 进行拖放。我想获取#drop 表内的td ID(data-id) 并将其存储到数组中(排序)?
table.html
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Story</th>
</tr>
</thead>
<tbody id="drop" class="sortables">
<tr>
<td data-id="67">67</td>
<td>this is test post for (news2)d</td>
</tr>
<tr>
<td data-id="59">59</td>
<td>how to use custom fonts in dja</td>
</tr>
<tr>
<td data-id="51">51</td>
<td>how can i merge two forms fiel</td>
</tr>
</tbody>
</table>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Story</th>
</tr>
</thead>
<tbody id="drag" class="sortables">
</tbody>
</table>
script.js
new Sortable(drag, {
group: '.sortables',
animation: 150,
});
new Sortable(drop, {
group: '.sortables',
animation: 150,
onChange: function (e, tr) {
sort = [];
$("#drog").children().each(function () {
sort.push({ 'id': $(this).data('id') })
});
console.log(sort)
},
});
【问题讨论】:
-
错字
$("#drog").children()=>$("#drop").children() -
请你解释一下我如何将它定义到我的函数中,因为我是 JavaScript 的初学者,你能通过更新我的代码中的代码来回答吗
-
换行:
$("#drog").children().each(function () {改为$("#drop").children().each(function () { -
是的,我也试试这个,但我得到
undefined或空数组
标签: javascript jquery bootstrap-4 sortablejs