Disable select2 plugin in JQuery dropdown

筅森魡賤 提交于 2019-12-08 03:41:46

问题


I am getting the values from json and displaying it in a popup. I need to disable that option which i am getting from json. How can i do that?

I tried normal jQuery but It doesn't work. I tried disabled true that also not working.

Please give solution.


回答1:


From the select2 docs

In case that you need to disable certain options so that they can't be selected by the select2 interface, you can now pass in disabled: true with your data. Please note: This also works for incoming values from ajax. When attached to a element, Select2 will respect its elements' disabled property.

$( document ).ready( function() {
    $( '#e24' ).select2( {
        query: function( query ) {
            var data = {
                results: [
                    { id: 1, text: "I'm selectable" },
                    { id: 2, text: "I'm a disabled option", disabled: true },
                    { id: 3, text: "I'm selectable too!" }
                ]
            };

            query.callback( data );
        }
    } );
} ); 


来源:https://stackoverflow.com/questions/27564245/disable-select2-plugin-in-jquery-dropdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!