【发布时间】:2016-05-22 10:13:14
【问题描述】:
我正在使用最新版本的 Select2。想要在下拉列表的末尾添加一个插入新按钮。我尝试了我在网上找到的这两个解决方案
解决方案 1:
$(".select2-drop").append('<table width="100%"><tr><td class="row"><button class="btn btn-block btn-default btn-xs" onClick="modal()">Add new Item</button></div></td></tr></table>');
解决方案 2
$("#itemId0").select2("container").find("div.select2-drop").append('<table width="100%"><tr><td class="row"><button class="btn btn-block btn-default btn-xs" onClick="modal()">Add new Item</button></div></td></tr></table>');
使用解决方案 1 没有任何反应。使用解决方案 2,我在选择 2 js 文件中收到错误消息
0x800a138f - JavaScript 运行时错误:无法获取属性“应用” 未定义或空引用
有人可以帮忙吗?
这是我的 HTML
<select id='itemId0' name='product[0][name]' class='form-control col-lg-5 itemSearch' >
<option></option>
</select>
以及完整的 select2 javascritp
function productFormatResult(product) {
if (product.loading) product.text;
var html = "<table><tr>";
html += "<td>";
html += product.text;
html += "</td></tr></table>";
return html;
}
// alert(html);
function productFormatSelection(product) {
var selected = "<input type='hidden' name='itemId' value='" + product.id + "'/>";
return selected + product.text;
}
//$("#itemId0").select2();
$("#itemId0").select2({
ajax: {
url: "@Url.Action("GetProducts", "Inventories")",
dataType: 'json',
data: function (params) {
return {
q: params.term
};
},
processResults: function (data, params) {
return { results: data };
},
cache: true
},
escapeMarkup: function (markup) { return markup; },
minimumInputLength: 1,
templateResult: productFormatResult,
templateSelection: productFormatSelection,
dropdownClass: 'bigdrop'
});
// $(".select2-drop").append('<table width="100%"><tr><td class="row"><button class="btn btn-block btn-default btn-xs" onClick="modal()">Add new Item</button></div></td></tr></table>');
$("#itemId0").select2("container").find("div.select2-drop").append('<table width="100%"><tr><td class="row"><button class="btn btn-block btn-default btn-xs" onClick="modal()">Add new Item</button></div></td></tr></table>');
【问题讨论】:
-
请给出完整的例子,html看起来如何?此实例中包含哪些 javascript 文件?
-
你想清楚了吗?我在获取 select2 的容器时遇到了完全相同的问题
-
这篇文章可能会有所帮助stackoverflow.com/questions/20977400/…
标签: jquery-select2 jquery-select2-4