Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

How to insert a variable into a javascript object for a context menu

I am trying to use jquery context menus on my grading web site. I have the following code:

   $.contextMenu({
    selector: 'span.context-menu',
        callback: function(key, options) {
         updatePerformancegradecontext(key,$(this).attr('id'));
        },
        items: {
            "1": {name: 'Beginning'}
            "2": {name: 'Developing'},
            "3": {name: 'Proficient'},
            "4": {name: 'Extending'},
            "sep1": "---------",
            "exc": {name: "Excused"},
            "abs": {name: "Absent"},
            "nhi": {name: "Not Handed In"},
            "empty": {name: "No Grade"}
        }
      });

I would like the first four items on the menu to be determined at runtime. When the webpage loads, I want to read some data from the page and create an object as follows:

    var p0 = new Object;
    p0.name=document.getElementById('perf0').innerHTML;

How do I insert this object into one of the items? I have tried many variations such as:

    items: {
        "1": p0,

and

    items: {
        "1": {p0},

etc. What am I missing? How can I create the items object using the p0 object as the name object? Or should I scrap javascript and just write this using HTML5 context menus?

question from:https://stackoverflow.com/questions/65952530/how-to-insert-a-variable-into-a-javascript-object-for-a-context-menu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...