jquery上下文菜单禁用菜单项

时间:2011-10-13 09:11:10

标签: jquery contextmenu

对于我的asp.net mvc 3应用程序,我正在使用this plugin。但我有一个问题,我想在创建菜单后启用或禁用某些菜单项,如:

    <script type="text/javascript">
    $(function(){
        /**************************************************
         * Menu 1
         **************************************************/
        $.contextMenu({selector: '.context-menu-one', items: {
            edit: {name: "Edit", icon: "edit", callback: $.noop, accesskey:"e d i t"},
            cut: {name: "Cut", icon: "cut", callback: $.noop, accesskey:"c u t"},
            copy: {name: "Copy", icon: "copy", callback: $.noop, accesskey:"c o p y"},
            paste: {name: "Paste", icon: "paste", callback: $.noop, accesskey:"p a s t e"},
            "delete": {name: "Delete", icon: "delete", callback: $.noop, accesskey:"d e l t"},
            sep1: "---------",
            quit: {name: "Quit", icon: "quit", callback: $.noop, accesskey:"q u i t"}
        }});

        //not working
        $('.context-menu-one').contextMenu('commands[0].disabled','true');
      });

  </script> 

不工作,有什么想法吗?

1 个答案:

答案 0 :(得分:13)

根据插件的documentation,您可以指定要调用的函数,以确定是否禁用了菜单项。

因此,您可以让该函数关闭局部变量,并更新该变量以启用或禁用这些项。类似的东西:

$(function() {
    var itemsDisabled = {};  // Enable everything initially.
    $.contextMenu({
        selector: ".context-menu-one",
        items: {
            // [...]
            cut: {
                name: "Cut",
                icon: "cut",
                callback: $.noop,
                accesskey: "c u t",
                disabled: function(key, opt) {
                    return !!itemsDisabled[key];
                }
            }
            // [...]
        }
    });

    // Disable the "Cut" menu item.
    itemsDisabled["cut"] = true;
});