如何在选择后删除选择的项目并在取消选择后将其取回
How to remove choosen item once it is selected and get it back once unselected
您好,我在实现选择 select/unselect 时遇到问题,我正在使用 https://harvesthq.github.io/chosen/
问题:我想在选中后从下拉列表中删除项目,并在取消选中后将其取回
这是我的图像显示问题:
下面是我选择的全部代码:
$('.chosen-select').chosen({}).change( function(obj, result) {
//console.debug("changed: %o", arguments);
console.log("selected: " + result.selected);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.min.css">
<div>
<select data-placeholder="Choose a Country..." class="chosen-select" multiple style="width:350px;" tabindex="4">
<option value=""></option>
<option value="Any">[Any]</option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Aland Islands">Aland Islands</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
</select>
</div>
li.result-selected { display:none !important; }
我试过的其他东西没有用,但是 CSS 更改工作正常:
$('.chosen-select').chosen({}).change(function(obj, result) {
console.log("selected: " + result.selected);
/* None of this works. The CSS does work
const vals = $(".chosen-select").val();
$('.chosen-select').find("option[value='"+result.selected+"']").remove()
console.log(vals)
$(".chosen-select").val(vals);
// $('.chosen-select').trigger("chosen:updated");
*/
});
li.result-selected {
display: none !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.min.css">
<div>
<select data-placeholder="Choose a Country..." class="chosen-select" multiple style="width:350px;" tabindex="4">
<option value=""></option>
<option value="Any">[Any]</option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Aland Islands">Aland Islands</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
</select>
</div>
这是一个相关问题jQuery Chosen - update select list without losing selections,但没有帮助
您好,我在实现选择 select/unselect 时遇到问题,我正在使用 https://harvesthq.github.io/chosen/
问题:我想在选中后从下拉列表中删除项目,并在取消选中后将其取回
这是我的图像显示问题:
下面是我选择的全部代码:
$('.chosen-select').chosen({}).change( function(obj, result) {
//console.debug("changed: %o", arguments);
console.log("selected: " + result.selected);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.min.css">
<div>
<select data-placeholder="Choose a Country..." class="chosen-select" multiple style="width:350px;" tabindex="4">
<option value=""></option>
<option value="Any">[Any]</option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Aland Islands">Aland Islands</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
</select>
</div>
li.result-selected { display:none !important; }
我试过的其他东西没有用,但是 CSS 更改工作正常:
$('.chosen-select').chosen({}).change(function(obj, result) {
console.log("selected: " + result.selected);
/* None of this works. The CSS does work
const vals = $(".chosen-select").val();
$('.chosen-select').find("option[value='"+result.selected+"']").remove()
console.log(vals)
$(".chosen-select").val(vals);
// $('.chosen-select').trigger("chosen:updated");
*/
});
li.result-selected {
display: none !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.min.css">
<div>
<select data-placeholder="Choose a Country..." class="chosen-select" multiple style="width:350px;" tabindex="4">
<option value=""></option>
<option value="Any">[Any]</option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Aland Islands">Aland Islands</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
</select>
</div>
这是一个相关问题jQuery Chosen - update select list without losing selections,但没有帮助