gpt4 book ai didi

jquery - 根据之前的选择禁用选项

转载 作者:行者123 更新时间:2023-12-01 01:14:44 25 4
gpt4 key购买 nike

我有四个<select>标签。它们如下:

<select name="" id="a">
<option value="0">Select</option>
<option value="1">Milk</option>
<option value="2">Eggs</option>
<option value="3">Cheese</option>
<option value="4">Butter</option>
<option value="5">Pizza</option>
</select>
<select name="" id="b">
<option value="0">Select</option>
<option value="1">Milk</option>
<option value="2">Eggs</option>
<option value="3">Cheese</option>
<option value="4">Butter</option>
<option value="5">Pizza</option>
</select>
<select name="" id="c">
<option value="0">Select</option>
<option value="1">Milk</option>
<option value="2">Eggs</option>
<option value="3">Cheese</option>
<option value="4">Butter</option>
<option value="5">Pizza</option>
</select>
<select name="" id="d">
<option value="0">Select</option>
<option value="1">Milk</option>
<option value="2">Eggs</option>
<option value="3">Cheese</option>
<option value="4">Butter</option>
<option value="5">Pizza</option>
</select>

这四个人应该有独特的选择,尽管他们有相同的元素集。那么使用 jQuery,如何禁用已选择的选项呢?准确地说,我需要从 <select> 中获取四个值。标签。

Solution and Problem

The current problem has a solution, but there's a limitation in it. Check my answer for the solution.

This solution is not scalable. If there are four <select>s, there should be four variables and the manual process to make them is tedious. Expecting a better solution.

最佳答案

每次选择更改时,只需循环遍历每个选择,然后在值匹配的所有其他选择中禁用该选项:

$('select').on('change', function() {

/* enable any previously disabled options */
$('option[disabled]').prop('disabled', false);

/* loop over each select */
$('select').each(function() {

/* for every other select, disable option which matches this this.value */
$('select').not(this).find('option[value="' + this.value + '"]').prop('disabled', true);

});

});

Here's a fiddle

关于jquery - 根据之前的选择禁用选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24909907/

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