[英]Update dropdown options based on selected value in jQuery multi-select select2
I am sending an array of objects from the backend to populate the select2 dropdown.我从后端发送一组对象来填充 select2 下拉列表。
my_array = [ {"id": "first_issue", "text": "First Issue"}, {"id": "second_issue", "text": "Second Issue"}, {"id": "no_issue", "text": "No Issue"} ] $('#issues').select2({ placeholder: 'Select Issue Type', allowClear: true, data: my_array });
#issues { width: 200px; }
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet"/> <div class="row"> <div class="columns medium-6"> <label for = "issues" style="text-align: left;" >Issues</label> <select class="js-select2" id="issues" name="issues" multiple></select> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
So I am populating the select2 box as above.所以我正在填充上面的 select2 框。 This select2 is
multi-select
dropdown.此 select2 是
multi-select
下拉列表。 Now what I need to do is to conditionally update the data
parameter of the select2 box so that whenever the option which is an issue ie, any option other than no_issue
is selected then the no_issue
option should not be shown in the dropdown and if it is already selected then it should be removed from the selection and vice versa should happen.现在我需要做的是有条件地更新 select2 框的
data
参数,以便无论何时选择有问题的选项,即选择no_issue
以外的任何选项,那么no_issue
选项不应显示在下拉列表中,如果它是已经选择然后它应该从选择中删除,反之亦然。 How can I do the same?我怎样才能做同样的事情?
Please let me know if this achieves what you want.如果这能达到您想要的效果,请告诉我。 You can test it out with the snippet below.
您可以使用下面的代码片段对其进行测试。
my_array = [ {"id": "first_issue", "text": "First Issue"}, {"id": "second_issue", "text": "Second Issue"}, {"id": "no_issue", "text": "No Issue"} ] $('#issues').select2({ placeholder: 'Select Issue Type', allowClear: true, data: my_array }); $("select").on("select2:select", function(evt) { let select = $(this); let remove = function(toRemove){ let values = select.val(); for (let i = 0; i < toRemove.length; i++) { let index = values.indexOf(toRemove[i]); if (index >= 0) { values.splice(index, 1); } } $("select").val(values).change(); }; let indexInDOM = $(evt.params.data.element).index(); if (indexInDOM === 2) { remove(["first_issue", "second_issue"]); } else { remove(["no_issue"]); } });
#issues { width: 200px; }
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet"/> <div class="row"> <div class="columns medium-6"> <label for = "issues" style="text-align: left;" >Issues</label> <select class="js-select2" id="issues" name="issues" multiple></select> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.