Select2 - Link on formatNoMatches

坚强是说给别人听的谎言 提交于 2019-12-10 13:56:42

问题


I'm using Select2 to enhance my <select> input. I populate the <options> with coldfusion and sql. I want to use formatNoMatches to add the option if there are no matches in the user search. I don't want to use a multiple select because it does not work with my coldfusion, plus it would be much simpler to simply add a click event to formatNoMatches.

Somewhere in select2.js, there is code that is stopping mouse events from occurring for formatNoMatches. Is there a way to hack it that will allow mouse events?

$(".select").select2({
    allowClear: true,
    blurOnChange: true,
    openOnEnter: false,
    formatNoMatches: function(term) {
        return "<a href='#' onclick='alert('" + term + "');'"
          + "id='newClient'>Add New Client</a>";
    }
});

I can't get the above code to alert anything.


回答1:


This is an issue with select2. There is a bug for the same. But till now no solution. Some idea is there, you can find the details below.

https://github.com/ivaynberg/select2/issues/276




回答2:


Use the select2 createSearchChoice function to allow the user to input their own option. Then use the change event on select2 to listen for any new search choices and launch a mini-form or unhide a link on the page to add additional details if a custom choice is selected.



来源:https://stackoverflow.com/questions/15070401/select2-link-on-formatnomatches

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