Jquery 在select2上搜索时调用ajax
我有两个select2组件,我想在搜索时绑定一个keyup事件。如何绑定到特定元素 唯一有效的方法是以下内容,但这会在两个select2项目上触发Jquery 在select2上搜索时调用ajax,jquery,jquery-select2,Jquery,Jquery Select2,我有两个select2组件,我想在搜索时绑定一个keyup事件。如何绑定到特定元素 唯一有效的方法是以下内容,但这会在两个select2项目上触发 $(document).on('keyup', '.select2-search__field', function (e) { // ajax call to get more product codes }); 包含所选项目的My cshtml: @Html.DropDownList("CatalogueCode", Enumerable
$(document).on('keyup', '.select2-search__field', function (e) {
// ajax call to get more product codes
});
包含所选项目的My cshtml:
@Html.DropDownList("CatalogueCode", Enumerable.Empty<SelectListItem>(), new { @class = "form-control" })
@Html.DropDownList("ProductCode", Enumerable.Empty<SelectListItem>(), new { @class = "form-control" })
可能与select2构造html代码的方式有关。必须通过以下方式将类添加到DropdownList:
$("#ProductCode").select2({
dropdownCssClass: "productCodesClass"
});
然后,如果我们想要获取用户在搜索字段中写入的值,下面的代码是正确的
$(document).on('keyup', '.productCodesClass > span.select2-search> input.select2-search__field', function (e) {
// ajax call to get more product codes
// $(this).val() is user's input
});
我想你错过了一个机会。或此选择器的前缀:“select2-search\u field”使用:选择器名称后的第一个子项或:第n个子项。select2-search\u field:n-child2“我也有同样的问题。它在两个选择中都激发,因为搜索字段输入只有在我在选择中单击时才会出现。