Select2: how to add a link instead of “No results found” text?

℡╲_俬逩灬. 提交于 2019-12-24 17:06:27

问题


Following is my code:

$('#cow_id_2').select2({
        allowClear: true,
        placeholder: "Search a cow/dam ID",
        formatNoMatches: function (term) {
            return "<a href=/'http://google.com/'>Add</a>";
        }
    });

When I try to add a link the plugin just stop working. I am using Select2 4.0.3 version


回答1:


If you're using version 4 or newer of select2, try this:

$('#cow_id_2').select2({
        allowClear: true,
        escapeMarkup: function (markup) { return markup; },
        placeholder: "Search a cow/dam ID",
        language: {
            noResults: function () {
                 return "<a href=/'http://google.com/'>Add</a>";
            }
        }
    });



回答2:


The selected answer is correct. Here is a little precision. Instead of overriding escapeMarkup, you can return a jQuery object in the noResults method. Like this :

$('#cow_id_2').select2({
    allowClear: true,
    placeholder: "Search a cow/dam ID",
    language: {
        noResults: function () {
            return $("<a href='http://google.com/'>Add</a>");
        }
    }
});


来源:https://stackoverflow.com/questions/37797597/select2-how-to-add-a-link-instead-of-no-results-found-text

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