[英]ListBox items remains after replace by empty array in jQuery
I have a list box which I want to clear by jQuery without page refresh. 我有一个列表框,我想通过jQuery清除而不刷新页面。
<asp:ListBox ID="lbComplaints" runat="server"
ClientIDMode="Static" SelectionMode="Multiple"
placeholder="Select Complaints" Style="width: 280px;" AppendDataBoundItems="True">
in a button click 在一个按钮中单击
$('input[type="submit"]').click(function (e) {
e.preventDefault();
$("#lbComplaints").val([]);
)};
It does not show any error but the items remain in previous selection. 它没有显示任何错误,但是项目保留在先前的选择中。
I tried 我试过了
$("#lbComplaints").empty();
but all the data of the Listbox is removed but I do not want to remove options but unselection of previous option in Listbox value so that user can select again. 但是删除了列表框的所有数据,但是我不想删除选项,而是取消选择列表框值中的上一个选项,以便用户可以再次选择。 How can I do that? 我怎样才能做到这一点?
By the way i used 顺便说一句
$('#lbComplaints').select2({
placeholder: 'select a state',
//tags: "true",
//allowClear: true,
theme: "classic"
});
您可以尝试执行以下操作将其重置:
$("#lbComplaints option:selected").removeAttr("selected");
Unselect 取消选择
$("#lbComplaints").find("option").attr("selected", false);
You are using select2.js. 您正在使用select2.js。 Modify your javascript as below 如下修改您的JavaScript
var $listX = $('#lbComplaints').select2({
placeholder: 'select a state',
//tags: "true",
//allowClear: true,
theme: "classic"
});
$('input[type="submit"]').click(function (e) {
e.preventDefault();
$listX.val(null);
)};
see the documentation here https://select2.github.io/examples.html#programmatic-control 在这里查看文档https://select2.github.io/examples.html#programmatic-control
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.