gpt4 book ai didi

javascript - 检查选择框选项重复次数是否超过特定次数

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

我有一个包含 html 的表单:

<input class="empcode" type="text">

<!--this select box options is fielled out from database but for example
data like this -->

<select class="select1">
<option value ="05-2018">05-2018</option>
<option value ="05-2018">05-2018</option>
<option value ="06-2018">05-2018</option>
</select>

<select class="result">
<option value= "1">1</option>
<option value= "1">1</option>
<option value= "1">1</option>
</select>

当“select1”类的选择框的月份值重复两次以上时,我需要禁用“结果”类的选择框我尝试了以下方法:

function HasDuplicateValues() {
var Duplicates = false;
$('#select1 option').each(function () {
var $SameValue = $('#select1 option[value=' + $(this).val() + ']');
Duplicates = $SameValue.length > 2);
alert('dublicates')

};
});

任何帮助!!

最佳答案

如果“.select1”有超过 2 个相同的选项,这将隐藏“.result”。

$('.select1 option').each(function (k,v) {
var value = $(v).attr('value');
var SameValue = $('.select1 option[value=' + value + ']');
if(SameValue.length > 2){
$('.result').hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="empcode" type="text">

<!--this select box options is fielled out from database but for example
data like this -->

<select class="select1">
<option value ="05-2018">05-2018</option>
<option value ="05-2018">05-2018</option>
<option value ="05-2018">05-2018</option>
</select>

<select class="result">
<option value= "1">1</option>
<option value= "1">1</option>
<option value= "1">1</option>
</select>

关于javascript - 检查选择框选项重复次数是否超过特定次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50337011/

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