单击jQuery多选选项,所有选项均消失

时间:2019-04-25 13:32:09

标签: jquery multi-select

我有两个函数可以向jquery多重选择添加选项,但是两者都表现异常。对于第一个,我无法取消选择任何选项。对于第二个,如果我单击一个,所有选项都消失了。

初始化

$("#addrBookNotiMsgRcpt").multiselect({
    selectedList: 1,
    selectedText: "# selected",
    noneSelectedText: "Recipient",
    close: function() {
        if ($("#addrBookNotiMsgRcpt > option").length === 0) {
            $("#abMsgActAll").notify(
                "Select recipient, maximum " + maxRecipient, "warn", {
                    position: "top right"
                }
            );
        }
    }
}).multiselectfilter();

问题:如果我添加如下选项,则无法从多选中取消选择任何选项

function updateRecipient() {
    let selectedRows = $('input[type="checkbox"]:checked:not(#abMsgActAll)', $('#addressBookMsgTable').DataTable().cells().nodes);
    $('#addrBookNotiMsgRcpt').empty().multiselect('refresh');
    let uniqueOption = [];
    let uniqueEmail = [];
    $.each(selectedRows, function() {
        let option = $("<option></option>");
        let data = $(this).data();
        if (!jQuery.isEmptyObject(data)) {
            option.html(data.username);
            option.attr("data-userid", data.userid);
            option.attr("data-firstname", data.firstname);
            option.attr("data-lastname", data.lastname);
            option.attr('data-email', data.email);
            option.attr('data-contactno', data.contactno);
            option.attr('selected', 'selected');
            option.val(data.userid);
            if (jQuery.inArray(data.email, uniqueEmail) === -1) {
                uniqueEmail.push(data.email);
                uniqueOption.push(option);
            }

        }
    });
    $.each(uniqueOption, function() {
        $("#addrBookNotiMsgRcpt").append($(this));
    });

    $('#addrBookNotiMsgRcpt').multiselect('refresh');
}

如果我仅单击一个,所有选项都会消失

$("#addrBookRcptSrchBtn").click(function() {
    $.post("retrieveWorkers", $("#addrBookRcptSrchForm").serialize()).done(function(response) {
        if (response.workerList) {
            $('#addrBookNotiMsgRcpt').empty().multiselect('refresh');
            $.each(response.workerList, function(i, worker) {
                let option = $("<option></option>");
                option.val(worker.usrerId).text(worker.fullName);
                $(option).data("userid", worker.usrerId);
                $(option).data("firstname", worker.firstName);
                $(option).data("lastname", worker.lastName);
                $(option).data("email", worker.emailId);
                $(option).data("contactno", worker.phoneNo);
                $("#addrBookNotiMsgRcpt").append(option)

            });
            $('#addrBookNotiMsgRcpt').multiselect('refresh');
        }
    });
});

0 个答案:

没有答案