生成的 html 代码 javascript 不适用于 jquery 可排序
Generated html code with javascript not working with jquery sortable
每次用户按下按钮时,我都会生成一个 table。生成的 table 应该是 sortable,尽管 jquery 的 sortable 仅在服务器端生成的 table 上起作用。
正在生成 tables:
<script>
$(document).on('click', '.addMenu', function() {
$('.empty-table').after('<table class="account-subheader"><tbody class="connectedSortable"><tr><td colspan=5></td></tr></tbody> </table>');
//$(".empty-table").append($(".account-subheader"));
});
</script>
排序table函数:
<script>
$(document).ready( function() {
$( ".connectedSortable" ).sortable({
connectWith: ".connectedSortable",
receive: function(event, ui) {
$.ajax({
// Some ajax calls
},
success: function(response) {
if (response.status == "success") {
console.log(response);
} else {
console.log(response);
}
}
});
}
}).disableSelection();
});
</script>
您需要在生成 HTML 后调用 sortable
函数。
尝试将 sortable
函数保留在 javascript 方法中,并从 document.ready
和 click
事件中调用该方法。
希望对您有所帮助。
每次用户按下按钮时,我都会生成一个 table。生成的 table 应该是 sortable,尽管 jquery 的 sortable 仅在服务器端生成的 table 上起作用。
正在生成 tables:
<script>
$(document).on('click', '.addMenu', function() {
$('.empty-table').after('<table class="account-subheader"><tbody class="connectedSortable"><tr><td colspan=5></td></tr></tbody> </table>');
//$(".empty-table").append($(".account-subheader"));
});
</script>
排序table函数:
<script>
$(document).ready( function() {
$( ".connectedSortable" ).sortable({
connectWith: ".connectedSortable",
receive: function(event, ui) {
$.ajax({
// Some ajax calls
},
success: function(response) {
if (response.status == "success") {
console.log(response);
} else {
console.log(response);
}
}
});
}
}).disableSelection();
});
</script>
您需要在生成 HTML 后调用 sortable
函数。
尝试将 sortable
函数保留在 javascript 方法中,并从 document.ready
和 click
事件中调用该方法。
希望对您有所帮助。