在查看了另一个答案之后,似乎我也需要在每次调用时都传递id字段,否则它将禁用输入。
修复它的示例代码:
$('.select2').select2({ placeholder: "Policy Name", minimumInputLength: 3, multiple: false, quietMillis: 100, id: function(bond){ return bond._id; }, ajax: { url: "http://localhost:3000/search", dataType: 'json', type: 'POST', data: function(term, page) { return { search: term, page: page || 1 } }, results: function(bond, page) { return {results: bond.results, more: (bond.results && bond.results.length == 10 ? true: false)} } }, formatResult: formatResult, formatSelection: formatSelection, initSelection: initSelection})编辑
既然这总是被推崇,我会详细说明。.select2()方法期望
id所有结果上都有唯一的字段。幸运的是,有一种解决方法。该
id选项接受如下功能:
function( <INDIVIDUAL_RESULT> ) { // Expects you to return a unique identifier. // Ideally this should be from the results of the $.ajax() call. }由于我的唯一标识符是
<RESULT>._id,所以我只是
return <RESULT>._id;



