Select2:如何添加 link 而不是 "No results found" 文本?

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

以下是我的代码:

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

当我尝试添加 link 时,插件停止工作。 我使用的是 Select2 4.0.3 版本

如果您使用的是版本 4 或更高版本的 select2,请尝试以下操作:

$('#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>";
            }
        }
    });

所选答案正确。这里有一点精度。您可以在 noResults 方法中 return 一个 jQuery 对象,而不是覆盖 escapeMarkup。像这样:

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