【问题标题】:Generated html code with javascript not working with jquery sortable使用 javascript 生成的 html 代码不适用于可排序的 jquery
【发布时间】:2018-06-24 08:10:25
【问题描述】:

每次用户按下按钮时,我都会生成一个表格。生成的表应该是可排序的,尽管 jquery 的可排序仅适用于服务器端生成的表。

生成表:

<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>

可排序功能:

<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>

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-ui-sortable


    【解决方案1】:

    您需要在生成 HTML 之后调用 sortable 函数。 尝试将 sortable 函数保留在 javascript 方法中,并从 document.readyclick 事件中调用该方法。

    希望这会有所帮助。

    【讨论】:

    • 效果很好!在页面加载和生成 HTML 表格时调用 sortable 函数。
    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 2020-07-16
    相关资源
    最近更新 更多