简体   繁体   中英

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

I am using jquery contextmenu plugin and here is my DEMO

Below is my code:

 <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 page

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

Using contextmenu

Question

1) I want to get txtID when I clicked on any menu from contextmenu because its id help me to understand which row update in database?

2) I have three table Section Sub group Other group So when click on section part update Section table in database same with other two table. I want to get which submenu click from either Section or Sub group or Other group So how to get it?

For your first question, take a look at options.$trigger . This is the element that activated the context menu. You can do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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