jQuery Chosen plugin: customize/format text of option in select

别等时光非礼了梦想. 提交于 2019-12-10 18:09:22

问题


I'm using a plugin in jquery called chosen for a select box with an autocomplete feature. http://harvesthq.github.com/chosen/

Where can I format the text of the options in the select box?

Is there a method in the chosen plugin for this? I've read the documentation and scanned the code but can't seem to find it.

Here is a screenshot of what I want to achieve. (this is made with another plugin 'selectmenu' but I want to do the same using 'chosen')


回答1:


There is no such feature at master branch in current time.

But there is this pull-request, wich is immediately what you need (currently it's for jquery only): https://github.com/harvesthq/chosen/pull/692

So you can get code from here. https://github.com/cjstewart88/chosen/tree/templates




回答2:


Here is another way to do it for now...

You can add special characters that will still look okay in a normal select, but not normally appear in the text. For example TITLE :: Description. Then find the :: and format using that. This example is designed for single select. For multiple you would probably just have to change the .chosen-single, or use another way to select it.

$('select[name="SELECT_NAME"]')
    .chosen({search_contains: true})
    .add('.chosen-search')
    .on('chosen:showing_dropdown keyup change', function() {
    $('.chosen-results .active-result, .chosen-single span').html(function() {
        var html = $(this).html();
        if (/ :: /.test(html)) {
            return '<strong>' + html.replace(' :: ', '</strong> <span style="font-size:70%;display:inline;font-weight: normal;">') + '</span>';
        } else {
            return html;
        }
    });
});

Your result in this example should be <strong>Title</strong> <span style="font-size:70%;display:inline;font-weight: normal;">Description</span>




回答3:


You can add width as a parameter to chosen

$("field").chosen({ width: '400px' })


来源:https://stackoverflow.com/questions/10734982/jquery-chosen-plugin-customize-format-text-of-option-in-select

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