簡體   English   中英

在jQuery中用空數組替換后,列表框項目仍然保留

[英]ListBox items remains after replace by empty array in jQuery

我有一個列表框,我想通過jQuery清除而不刷新頁面。

<asp:ListBox ID="lbComplaints" runat="server" 
ClientIDMode="Static" SelectionMode="Multiple" 
placeholder="Select Complaints" Style="width: 280px;" AppendDataBoundItems="True">

在一個按鈕中單擊

$('input[type="submit"]').click(function (e) {
        e.preventDefault(); 
    $("#lbComplaints").val([]);    
)};

它沒有顯示任何錯誤,但是項目保留在先前的選擇中。

我試過了

 $("#lbComplaints").empty();

但是刪除了列表框的所有數據,但是我不想刪除選項,而是取消選擇列表框值中的上一個選項,以便用戶可以再次選擇。 我怎樣才能做到這一點?

順便說一句

$('#lbComplaints').select2({
        placeholder: 'select a state',
        //tags: "true",
        //allowClear: true,
        theme: "classic"
    });

您可以嘗試執行以下操作將其重置:

 $("#lbComplaints option:selected").removeAttr("selected");

取消選擇

$("#lbComplaints").find("option").attr("selected", false);

您正在使用select2.js。 如下修改您的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);
    )};

在這里查看文檔https://select2.github.io/examples.html#programmatic-control

但發現類似的答案

鏈接

由於我正在使用select2 jQuery無法清除選定的項目,因此使用select2 $('#lbComplaints').select2('data', null); 這完美地工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM