gpt4 book ai didi

javascript - 如何自动选择多选框chzn-select-deselect?

转载 作者:太空狗 更新时间:2023-10-29 16:41:59 25 4
gpt4 key购买 nike

我有一个多选 chzn-select-deselect 框。当我选择一个特定值时,我想一次选择多个值。我有以下 HTML:

<select id="filter_list_dropdwn" class="inp direct_value_opp fl" multiple="multiple" name="data[value1][]">
<option value="1" parent_id="0"> parent1</option>
<option value="2" parent_id="1"> child1 of parent1</option>
<option value="3" parent_id="1"> child2 of parent1</option>
<option value="4" parent_id="3"> child of child3</option>
</select>

如果我选择 parent1,那么它的 child 将自动被选中。工作脚本是这样的:

    $('#filter_list_dropdwn option:not(:selected)').live('click', function () {
unselected = $(this);

var parent_id = $(unselected).attr("value");
$('#filter_list_dropdwn option[parent_id=' + parent_id + ']').each(function (i, selected) {
$(this).prop('selected', false).click();
});
});

$('#filter_list_dropdwn option:selected').live('click', function () {
selected = $(this);
var parent_id = $(selected).attr("value");
$('#filter_list_dropdwn option[parent_id=' + parent_id + ']').each(function (i, selected) {
$(this).prop('selected', true).click();
});
});

这是上述功能的 fiddle :http://jsfiddle.net/NEXv3/

现在,我想在 chzn-select-deselect 选项中应用相同的内容。所以,我修改了脚本如下:

$('#filter_list_dropdwn option:not(:selected)').live('click', function () {
unselected = $(this);

var parent_id = $(unselected).attr("value");
$('#filter_list_dropdwn option[parent_id=' + parent_id + ']').each(function (i, selected) {
$(this).attr('selected', false).chosen();
});
});

$('#filter_list_dropdwn option:selected').live('click', function () {
selected = $(this);
var parent_id = $(selected).attr("value");
$('#filter_list_dropdwn option[parent_id=' + parent_id + ']').each(function (i, selected) {
$(this).attr('selected', true).chosen();
});
});

但它并没有像预期的那样工作。谁能告诉我在 chzn-select-deselect 下拉菜单中应用相同的自动多选选项出了什么问题?

最佳答案

快速浏览 author website ,您会看到选择时会有以下事件:

$("#form_field").chosen().change( … );

要更新选择下拉列表,您必须执行以下操作:

$("#form_field").trigger("chosen:updated");

现在,您的代码必须从头开始重写:

$('#filter_list_dropdwn').chosen(); // to apply the plugin

$("#filter_list_dropdwn").chosen().change(function(e, option){
// when changing, option will contain {selected: "value"} or {deselected: "value"}
var selected = option.selected;
var deselected = option.deselected;

if (selected !== undefined) {
// if we have selected a new option
$('#filter_list_dropdwn option[data-parent_id=' + selected + ']').each(function () {
$(this).prop('selected', true);
});
} else if(deselected !== undefined) {
// if we have deselected an option
$('#filter_list_dropdwn option[data-parent_id=' + deselected + ']').each(function () {
$(this).prop('selected', false);
});
}
// redraw the "chosen select" when all changes have been made to the real one
$("#filter_list_dropdwn").trigger("chosen:updated");
});

Demo jsFiddle

编辑:

代码可以更短:

$("#filter_list_dropdwn").chosen().change(function(e, option){
var parent_id = option.selected !== undefined ? option.selected : option.deselected;

$('#filter_list_dropdwn option[data-parent_id=' + parent_id + ']').each(function () {
$(this).prop('selected', option.selected !== undefined ? true : false);
});

$("#filter_list_dropdwn").trigger("chosen:updated");
});

Demo jsFiddle

编辑#2:

递归实现:

Final demo jsFiddle

关于javascript - 如何自动选择多选框chzn-select-deselect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22779132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com