jQuery的文本菜单 - 如何使用文本菜单操作来获取点击的项目的ID在

问题描述:

我使用jquery contextmenu plugin菜单,这里是我的DEMOjQuery的文本菜单 - 如何使用文本菜单操作来获取点击的项目的ID在

下面是我的代码:

<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" } 
       } } } }); }); 

enter image description here

cshtml页面

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

使用文本菜单

问题

1)我想txtID当我点击任何菜单上从文本菜单,因为它id帮助我了解数据库哪一行更新?

2)我有三个表SectionSub groupOther group所以当点击部分更新Section表中的数据库与其他两个表相同时。我想要获得从SectionSub groupOther group 中点击哪个子菜单,那么如何获取它?

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

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