使用 jquery 限制基于父级多重选择的子级多重选择

发布于 2024-09-28 08:57:55 字数 1632 浏览 1 评论 0原文

我需要创建一个界面,在选择父多项选择时,它会更新选项的子多项选择。其目的是父级多选可以不选择一个、一个或多个选项,并且子元素显示父级都拥有的项目的并集(或回退到所有项目)。

例如:

  • 当选择父级 1 时,应允许选择父级 1 的所有状态项
  • 当选择父级 1 和父级 2 时,应允许选择父级 1 和父级 2 中允许的所有状态项child 元素

在这里搜索并进行大量谷歌搜索后,我想我已经接近了,但不幸的是,这个解决方案并没有完全满足第二个要求。

我可能以完全错误的方式处理这个问题,所以我愿意接受建议。

JS:

$(document).ready(function(){

var allOptions = $('#children option').clone();

function children_update() {
    $('#parents > option:selected').each(function() { 
        $('#children').html(allOptions.filter('.option-' + parseInt($(this).val())));    
    });
    if(!$('#parents > option:selected').length) { $('#children').html(allOptions); }
}

children_update();

$('#parents').change(function() { children_update(); });
});

HTML

    Parents<br />
<select multiple name="parents[]" id="parents">
<option value="1">parent 1</option>
<option value="2">parent 2</option>
<option value="3">parent 3</option>
</select>

<br />
Children<br />
<select multiple name="children[]" id="children">
<option value="1" class="option-1">child 1 (1)</option>
<option value="2" class="option-1 option-2">child 2 (1,2)</option>
<option value="3" class="option-1 option-2 option-3">child 3 (1,2,3)</option>
<option value="4" class="option-1 option-2">child 4 (1,2)</option>
<option value="5" class="option-2">child 5 (2)</option>
<option value="6" class="option-3">child 6 (3)</option>
</select>

谢谢

I need to create an interface that upon selection of a parent multi select, it updates a child multi select of options. It is intended that the parent multi select can have none, one or many options selected and the child element displays a union of items that both parents have (or falls back to all items).

Take for example:

  • When parent 1 is selected, all status items for parent 1 should be allowed to be selected
  • When parent 1 and parent 2 are selected all status items that are allowed within both parent 1 and 2 should be allowed to be selected in the child element

After searching around here and doing a lot of googling I think I have got close, but this solution unfortunately does not fully satisfy the second requirement.

I may be approaching this totally the wrong way, so I'm open to suggestions.

The JS:

$(document).ready(function(){

var allOptions = $('#children option').clone();

function children_update() {
    $('#parents > option:selected').each(function() { 
        $('#children').html(allOptions.filter('.option-' + parseInt($(this).val())));    
    });
    if(!$('#parents > option:selected').length) { $('#children').html(allOptions); }
}

children_update();

$('#parents').change(function() { children_update(); });
});

The HTML

    Parents<br />
<select multiple name="parents[]" id="parents">
<option value="1">parent 1</option>
<option value="2">parent 2</option>
<option value="3">parent 3</option>
</select>

<br />
Children<br />
<select multiple name="children[]" id="children">
<option value="1" class="option-1">child 1 (1)</option>
<option value="2" class="option-1 option-2">child 2 (1,2)</option>
<option value="3" class="option-1 option-2 option-3">child 3 (1,2,3)</option>
<option value="4" class="option-1 option-2">child 4 (1,2)</option>
<option value="5" class="option-2">child 5 (2)</option>
<option value="6" class="option-3">child 6 (3)</option>
</select>

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2024-10-05 08:57:55

如何将 Children_update 函数更改为这个...

function children_update() {
    $("#children option").attr('disabled','true');   //start off with all disabled
    $('#parents > option:selected').each(function() {
        var num = $(this).val();
        $("#children .option-"+num).attr('disabled','false');
    });
    if(!$('#parents > option:selected').length){           //if no parents selected
        $("#children option").attr('disabled','false');    //enable all options
    }
}

如果我正确理解问题,那应该可行。但是,如果您正在做更严格的组合操作(例如仅显示与父选项的确切组合相匹配的子选项),那么请随时纠正我。

编辑:

好的,这是一个新的解决方案:

function children_update() {
    $("#children option").attr('disabled','true');   //start off with all disabled
    var selectors = getSelectors();
    $("#children option"+selectors).attr('disabled','false');
}

function getSelectors(){
    var parents = $("#parents").val();
    var selectors = "";
    if (parents){
        $.each(parents, function(index, val){
            selectors = selectors + ".option-" + val;
        });
    }
    return selectors;
}

How about changing the children_update function to this...

function children_update() {
    $("#children option").attr('disabled','true');   //start off with all disabled
    $('#parents > option:selected').each(function() {
        var num = $(this).val();
        $("#children .option-"+num).attr('disabled','false');
    });
    if(!$('#parents > option:selected').length){           //if no parents selected
        $("#children option").attr('disabled','false');    //enable all options
    }
}

If I understand the problem correctly, that should work. However, if you're doing a more strict combinatorial thing (like only showing child options that match the exact combination of parent options), then please feel free to correct me on this.

EDIT:

Okay, here's a new solution:

function children_update() {
    $("#children option").attr('disabled','true');   //start off with all disabled
    var selectors = getSelectors();
    $("#children option"+selectors).attr('disabled','false');
}

function getSelectors(){
    var parents = $("#parents").val();
    var selectors = "";
    if (parents){
        $.each(parents, function(index, val){
            selectors = selectors + ".option-" + val;
        });
    }
    return selectors;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文