jQuery 上下文菜单复选框已选中

jQuery contextMenu checkbox selected

我正在使用 SWIS 的 contexMenu,我不知道如何从复选框中更改选定的值。 我有这个代码:

function isOk(){
    return false;
}

$.contextMenu({
    selector: "td[name*='someSelector']", 
    callback: function(key, options) {
        var m = "clicked: " + key + " id: "+ this.attr('id');
        window.console && console.log(m) || alert(m);
    },
    items: {
        "okOption": {
            name: "ok", 
            type: 'checkbox', 
            selected: function(){ return isOk(); }
        },
        "quit": {
            name: "Close Menu", 
            icon: function(){
                return 'context-menu-icon context-menu-icon-quit';
            }
        }
    }
});

但复选框始终为真。

我不知道哪里出了问题,或者如果选择的函数不适用于某个函数,并且只适用于 true 或 false。

function isOk(){
   return false;
  }
  
  $.contextMenu({
   selector: "[name*='someSelector']", 
   callback: function(key, options) {
    var m = "clicked: " + key + " id: "+ this.attr('id');
    window.console && console.log(m) || alert(m);
   },
   items: {
    "okOption": {
     name: "ok", 
     type: 'checkbox', 
     selected: {function(){ return isOk(); }}
    },
    "quit": {name: "Close Menu", icon: function(){
     return 'context-menu-icon context-menu-icon-quit';
     }
    }
   }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.js"></script>
<script src="https://swisnl.github.io/jQuery-contextMenu/js/theme.js"></script>
<link href="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.css" rel="stylesheet"/>

<span name="someSelector"> ClickRightHere</span>

您不能为 selected 选项使用函数。 documentation 表示:

selected: string or boolean

所以你可以使用这样的变量:

var myFlag = false;

$.contextMenu({
    ...
    items: {
        "okOption": {
            name: "ok", 
            type: 'checkbox', 
            selected: myFlag
        },
    ...

最后我用一个事件解决了: 这里有解决方案的片段,供以后的人参考和解决。

function isOk() {
  return false;
}

$.contextMenu({
  selector: "[name*='someSelector']",
  callback: function(key, options) {
    var m = "clicked: " + key + " id: " + this.attr('id');
    window.console && console.log(m) || alert(m);
  },
  items: {
    "okOption": {
      name: "ok",
      type: 'checkbox'
    },
    "quit": {
      name: "Close Menu",
      icon: function() {
        return 'context-menu-icon context-menu-icon-quit';
      }
    }
  },
  events: {
    show: function(opt) {
      // this is the trigger element
      var $this = this;
      // import states from data store 
      $this.data().okOption = isOk();

      $.contextMenu.setInputValues(opt, $this.data());
      // this basically fills the input commands from an object
      // like {name: "foo", yesno: true, radio: "3", &hellip;}
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.js"></script>
<script src="https://swisnl.github.io/jQuery-contextMenu/js/theme.js"></script>
<link href="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.css" rel="stylesheet" />

<span name="someSelector"> ClickRightHere</span>