naq*_*b83 1 jquery-select2 jquery-select2-4 select2
以下是我的代码:
$('#cow_id_2').select2({
allowClear: true,
placeholder: "Search a cow/dam ID",
formatNoMatches: function (term) {
return "<a href=/'http://google.com/'>Add</a>";
}
});
Run Code Online (Sandbox Code Playgroud)
当我尝试添加链接时,插件就会停止工作.我使用的是Select2 4.0.3版本
G M*_*ros 11
如果您使用的是版本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>";
}
}
});
Run Code Online (Sandbox Code Playgroud)
所选答案是正确的。这里有一点精确。escapeMarkup您可以在方法中返回 jQuery 对象,而不是重写noResults。像这样\xc2\xa0:
$(\'#cow_id_2\').select2({\n allowClear: true,\n placeholder: "Search a cow/dam ID",\n language: {\n noResults: function () {\n return $("<a href=\'http://google.com/\'>Add</a>");\n }\n }\n});\nRun Code Online (Sandbox Code Playgroud)\n