【问题标题】:jQuery-ui sortable - Again enable sortable after it has been disabledjQuery-ui sortable - 禁用后再次启用可排序
【发布时间】:2014-07-05 08:39:52
【问题描述】:

我有以下 jQuery 代码:

var isOk = true;
$('#edit').click(function () {
    if (isOk) {
        $('table tbody').sortable({
            axis: 'y',
            update: function (event, ui) {
                var data = $(this).sortable('serialize');
                $.ajax({
                    data: data,
                    type: 'POST',
                    url: 'updatePagesOrder.php',
                    success: function (msg) { //re-number the rows from 1 to n
                        $('table > tbody tr').each(function (i) {
                            $(this).attr('id', 'item-' + (i + 1)); // use $(this) as a reference to current tr object
                        });
                    },
                    error: function () {
                        alert("An error occurred");
                    }
                });
            }
        }, "enable");
    }
    else {
        $("table tbody").unbind();
        $('table a').unbind();
    }
    isOk = !isOk;

在上面的代码中,#edit 是一个按钮,第一次点击它会导致表格行可排序,第二次点击它会禁用可排序选项。

我希望在第三次单击时,行将再次可排序。 上面的代码我试过了,还是不行。

为什么?我该如何解决?谢谢!

【问题讨论】:

  • 不知道为什么这被否决了...好问题

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


【解决方案1】:

在点击处理程序之外初始化小部件:

$('table tbody').sortable({
    disabled: true, // Initially disabled
    axis: 'y',
    update: function (event, ui) {
        var data = $(this).sortable('serialize');
        $.ajax({
            data: data,
            type: 'POST',
            url: 'updatePagesOrder.php',
            success: function (msg) { //re-number the rows from 1 to n
                $('table > tbody tr').each(function (i) {
                    $(this).attr('id', 'item-' + (i + 1)); // use $(this) as a reference to current tr object
                });
            },
            error: function () {
                alert("An error occurred");
            }
        });
    }
});

然后只需在单击按钮时切换“禁用”选项。

$("#edit").click(function() {
    var table = $('table tbody');
    table.sortable('option', 'disabled', !table.sortable('option', 'disabled'));
});

【讨论】:

  • 谢谢,但我得到一个错误:“'undefined is not a function”
  • 你加载了jQuery-UI库吗?
  • 在第一行,是的
  • 这个错误意味着它没有找到sortable 方法,我无法解释为什么如果成功加载了 jQuery 和 jQuery UI 库会发生这种情况。你还有其他使用$ 作为主要功能的库吗?您可能需要使用jQuery.noConflict()
  • 这里有一个fiddle,请您看看有什么问题吗? jsfiddle.net/es9BH/1
猜你喜欢
  • 2010-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
  • 2012-04-22
  • 2013-03-16
  • 1970-01-01
相关资源
最近更新 更多