gpt4 book ai didi

html - 如果在另一个选择中选择了此选项,如何禁用选择中的选项

转载 作者:行者123 更新时间:2023-12-01 07:37:09 27 4
gpt4 key购买 nike

我有三个具有相同选项的选择组。如果在另一个选择中选择了此选项,如何禁用选择中的选项?

                        <select class="form-control" id="first_select" name="foo" required>
<option disabled selected value="0">Choose*</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select class="form-control" id="second_select" name="foo2" required>
<option disabled selected value="0">Choose*</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4>4</option>
<option value="5">5</option>
</select>
<select class="form-control" id="third_select" name="foo3" required>
<option disabled selected value="0">Choose*</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4>4</option>
<option value="5">5</option>
</select>

最佳答案

你可以这样做:

$(".form-control").on("change", function() {
let selects = $(".form-control").not(this);
let allSelects = $(".form-control");
selects.each(function() {
let options = $(this).find("option");
options.each(function() {
$(this).prop("disabled", false);
});
});

allSelects.each(function() {
let val = $(this).val();
$(".form-control").not(this).find("option[value='" + val + "']").prop("disabled", true);

});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control" id="first_select" name="foo" required>
<option disabled selected value="0">Choose*</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select class="form-control" id="second_select" name="foo2" required>
<option disabled selected value="0">Wybierz zawód*</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select class="form-control" id="third_select" name="foo3" required>
<option disabled selected value="0">Choose*</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>

关于html - 如果在另一个选择中选择了此选项,如何禁用选择中的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61753131/

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