使用 jquery 限制基于父级多重选择的子级多重选择
我需要创建一个界面,在选择父多项选择时,它会更新选项的子多项选择。其目的是父级多选可以不选择一个、一个或多个选项,并且子元素显示父级都拥有的项目的并集(或回退到所有项目)。
例如:
- 当选择父级 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何将 Children_update 函数更改为这个...
如果我正确理解问题,那应该可行。但是,如果您正在做更严格的组合操作(例如仅显示与父选项的确切组合相匹配的子选项),那么请随时纠正我。
编辑:
好的,这是一个新的解决方案:
How about changing the children_update function to this...
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: