简体   繁体   中英

Jquery contextMenu plugin fails to accept default selection

Utilizing the example here

 select: {
                name: "Select", 
                type: 'select', 
                options: {1: 'one', 2: 'two', 3: 'three'}, 
                selected: 2
            }

refuses to have the default selection of the drop down menu be two and instead leaves it as one . We are using jQuery 1.8, is there a known issue with the above code on this version or is there some workaround that can be put in place? The issue even persists on the demo page.

Environment:
jQuery 1.8
Firefox 19.0

working fiddle

I checked the plugin, I think it never worked with selected value of the select, but it use the current value only a run time.

This is because the setInputValues function set the selected property if is it undefined in the data object. I have fixed the code by setting the property only if the data object is not undefined.

See:

// import values into <input> commands
$.contextMenu.setInputValues = function(opt, data) {
    if (data === undefined) {
        data = {};
    }

    $.each(opt.inputs, function(key, item) {
        switch (item.type) {
            case 'text':
            case 'textarea':
                item.value = data[key] || "";
                break;

            case 'checkbox':
                item.selected = data[key] ? true : false;
                break;

            case 'radio':
                item.selected = (data[item.radio] || "") == item.value ? true : false;
                break;

            case 'select':
                if (data[key]!=undefined ){
                  item.selected = data[key] || "";
                }
                break;
        }
    });
};

Working fiddle: http://jsfiddle.net/vYnv3/1/

Here is a pastebin of the fixed code: http://pastebin.com/Mg3j7ifB

If works weel I'll fork the fix.

EDIT

Added support for radio and checkbox too:

// import values into <input> commands
$.contextMenu.setInputValues = function(opt, data) {
    if (data === undefined) {
        data = {};
    }

    $.each(opt.inputs, function(key, item) {
        switch (item.type) {
            case 'text':
            case 'textarea':
                item.value = data[key] || "";
                break;

            case 'checkbox':
                if (data[key]!=undefined ){
                    item.selected = data[key] ? true : false;
                }
                break;

            case 'radio':
                if (data[item.radio]!=undefined ){
                    item.selected = (data[item.radio] || "") == item.value ? true : false;
                }
                break;

            case 'select':
                if (data[key]!=undefined ){
                  item.selected = data[key] || "";
                }
                break;
        }
    });
};

New pastebin: http://pastebin.com/c8XFVMiD

Working fiddle: http://jsfiddle.net/vYnv3/2/

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