[英]Set all options-tags to “disabled” when they match with array value (jQuery)
I struggle a bit with a problem where I have to use two for loops (that's my approach at least), to compare data with an array. 我有点困难,我必须使用两个for循环(这至少是我的方法),以比较数据与数组。 I'll try to keep it short:
我会尽量保持简短:
I have an array var excludeTimes = ["00:00", "00:45", "02:30", "14:15"] etc.
我有一个数组
var excludeTimes = ["00:00", "00:45", "02:30", "14:15"] etc.
and an <select>
element with some <option>
elements 以及带有
<option>
元素的<select>
<option>
元素
<select id="Times">
<option value="--:--">--:--</option>
<option value="00:00">00:00</option>
<option value="00:15">00:15</option>
<option value="00:30">00:30</option>
<option value="00:45">00:45</option>
etc.
</select>
Now, I want to compare the values in my excludeTimes
array with the values of my <option>
tags in <select>
. 现在,我想将
excludeTimes
数组中的值与<select>
<option>
标记的值进行比较。
If they match, add the attribute "disabled" to the matched 如果匹配,则将匹配的属性“禁用”添加
function notSelectableTimes() {
const excludeTimes = ["00:00", "00:45", "02:30", "14:15"]
const e = $("#Times>option").map(function() { return $(this).val() }).get();
const ee = e.slice(1);
for(i = 0; i < excludeTimes.length; i++) {
const find = $.inArray( excludeTimes[i], ee);
for(j = 0; j < ee.length; j++) {
if( ee[j] === excludeTimes[i]) {
console.log(`No bookings at ${ee[j]}!`);
console.log(ee[j]);
console.log(find);
}
}
}
}
notSelectableTimes();
The route you're taking is a bit convoluted in my opinion. 在我看来,你所走的路线有点复杂。 You loop through the options to map them to their values, then compare them to the array, and then hypothetically would need to re-lookup the options by value to then disable them .
您遍历选项以将它们映射到它们的值,然后将它们与数组进行比较,然后假设需要按值重新查找选项然后禁用它们 。
The last step is the portion you're missing, but rather than complete that route, I'd prefer to propose an alternative. 最后一步是你缺少的那一部分,但我没有完成那条路线,我宁愿提出一个替代方案。 You're already looping through the options to
.map()
them - why not just compare the values in that loop instead? 您已经将选项循环到
.map()
它们 - 为什么不直接比较该循环中的值?
The following loops through each <option>
in #Times
and sets its disabled
attribute based on the presence of the option's value in the excludeTimes
array. 以下循环遍历
#Times
每个<option>
,并根据excludeTimes
数组中选项值的存在设置其disabled
属性。
const excludeTimes = ["00:00", "00:45", "02:30", "14:15"]; $("#Times > option").attr("disabled", function() { //For each <option> in #Times return excludeTimes.includes($(this).val()); //Disable if value in excludeTimes });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="Times"> <option value="--:--">--:--</option> <option value="00:00">00:00</option> <option value="00:15">00:15</option> <option value="00:30">00:30</option> <option value="00:45">00:45</option> </select>
In your code you need to add a line as below 在您的代码中,您需要添加一行,如下所示
$('#Times option[value="'+ee[j]+'"]').attr('disabled','disabled'); $('#Times选项[value =“'+ ee [j] +'']')。attr('禁用','禁用');
<script>
$(document).ready(function(){
notSelectableTimes();
});
function notSelectableTimes() {
const excludeTimes = ["00:00", "00:45", "02:30", "14:15"]
const e = $("#Times>option").map(function() { return $(this).val() }).get();
const ee = e.slice(1);
for(i = 0; i < excludeTimes.length; i++) {
const find = $.inArray( excludeTimes[i], ee);
for(j = 0; j < ee.length; j++) {
if( ee[j] === excludeTimes[i]) {
$('#Times option[value="'+ee[j]+'"]').attr('disabled','disabled');
console.log(`No bookings at ${ee[j]}!`);
console.log(ee[j]);
console.log(find);
}
}
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.