繁体   English   中英

jQuery contextmenu-如何使用contextmenu操作获取菜单中单击项的ID

[英]jquery contextmenu - how to use contextmenu action to get id of the clicked item in the menu

我正在使用jquery contextmenu插件 ,这是我的DEMO

下面是我的代码:

 <script type="text/javascript">
$(function () {

    $.contextMenu({
        selector: '.context-menu-one',
        callback: function (key, options) {           
            var m = "clicked: " + key;
            window.console && console.log(m) || alert(m);                        
            alert("Clicked on " + key + " on element " + options.$trigger.attr('id').substr(3));

        },
        items: {
            "edit": { "name": "Edit", "icon": "edit" },           
            "fold1": {
                "name": "Apply for Section",
                "items": {
                    "Section I": { "name": "Section I" },
                    "Section II": { "name": "Section II" },
                    "Section III": { "name": "Section III" }
                }
            },
            "fold2": {
                "name": "Sub group",
                "items": {
                    "fold1-key1": { "name": "Foo bar" },
                    "fold2": {
                        "name": "Sub group 2",
                        "items": {
                            "fold2-key1": { "name": "Sub group 1" },
                            "fold2-key2": { "name": "Sub group 2" },
                            "fold2-key3": { "name": "Sub group 3" }
                        }
                    },
                    "fold1-key3": { "name": "delta" }
                }
            },
            "fold1a": {
                "name": "Other group",
                "items": {
                    "fold1a-key1": { "name": "Other group1" },
                    "fold1a-key2": { "name": "Other group2" },
                    "fold1a-key3": { "name": "Other group3" }
                } }  }  }); });

在此处输入图片说明

cshtml页面

@foreach(var id in data)
  { 
 <div class="context-menu-one">
      <input type="hidden" id="txtID" value="@id">
  </div>
  <br/>
 } 

使用上下文菜单

1)我想从txtID菜单中单击任何菜单时获取txtID ,因为它的id帮助我了解数据库中的哪一行更新?

2)我有三个表Section Sub group Other group因此,当单击section部分更新数据库中的Section表时,与其他两个表相同。 我想从“ Section或“ Sub group或“ Other group获取哪个子菜单,那么如何获得它?

对于第一个问题,请看一下options.$trigger 这是激活上下文菜单的元素。 你可以做:

 var el = options.$trigger;
 var id = el.attr('id');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM