简体   繁体   English

当所有options-tags与数组值(jQuery)匹配时,将它们设置为“disabled”

[英]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.

相关问题 jQuery:如果值在数组中,则在选项中设置“ selected”标记 - jQuery: set 'selected' tag in options if value is in array 如何在没有jQuery的情况下启用所有禁用的表单选择选项? - How to enable all disabled form select options without jQuery? Javascript - 当对象的属性值与选项数组中的所有值匹配时,遍历一组选项并返回一个对象? - Javascript - Loop through an array of options and return an object when the object's property values match all values in the options array? Yii2,jQuery:.prop(&#39;disabled&#39;,true); 当value =“ x”时的所有字段 - Yii2, jQuery : .prop('disabled', true); all field when value=“x” jQuery - 将select的值设置为其中一个选项 - jQuery - set the value of a select to one of its options JS数组中一组输入标签的值 - Value of set of input tags in a JS array 从jQuery for ASP.NET设置禁用输入的值 - Set value of disabled input from jQuery for ASP.NET jQuery何时; 值未设置 - Jquery when ; value not set 由于“X-frame-options”设置为“deny”,Facebook 评论在网站的所有页面上被禁用 - Facebook comments disabled on all pages of website due to "X-frame-options" set to "deny" JavaScript检查下拉选项是否全部禁用 - JavaScript check if dropdown options are all disabled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM