简体   繁体   English

使用复选框禁用多项选择

[英]Disable multiple selects with a checkbox

My code is a mess, sorry about that. 我的代码一团糟,对此感到抱歉。

The JS works but only for the first select; JS有效,但仅适用于第一次选择。 I want the user to input a time, but if the user does not work that day, I want to disable the four select boxes. 我希望用户输入时间,但是如果用户当天不工作,则要禁用四个选择框。 Unfortunately, I don't know where to add the other selects in the JS. 不幸的是,我不知道在JS中添加其他选择的位置。

There's another script that fills in the rest of the options for the selects. 还有另一个脚本可以填充选择的其余选项。 The code I have so far: 我到目前为止的代码:

 if (document.getElementById('holiday_check').checked) document.getElementById('holiday_time_h1').setAttribute('disabled', 'disabled'); function closed_holiday() { if (document.getElementById('holiday_check').checked) document.getElementById('holiday_time_h1').setAttribute('disabled', 'disabled'); else document.getElementById('holiday_time_h1').removeAttribute('disabled'); } 
 <div> <span>Opening Time:</span> <select id="holiday_time_h1"><option>00</option></select> : <select id="holiday_time_m1"><option>00</option></select> <span>Closing time</span> <select id="holiday_time_h2"><option>00</option></select> : <select id="holiday_time_m2"><option>00</option></select> <input id="holiday_check" class="check" type="checkbox" onchange="closed_holiday();">Closed</input> </div> 

There is multiple ways to abort this problem. 有多种方法可以中止此问题。 The most common and abordable way is to loop through each select and disable them one by one. 最常见且可厌的方法是遍历每个选择,然后逐个禁用它们。

First, you need to get an array for each select element that you want to disable. 首先,您需要为要禁用的每个选择元素获取一个数组。 You can attach to them the same class; 您可以给他们附加相同的课程; let's say "holiday", then get all of them in an array using the selector getElementsByClassName() or querySelector() . 让我们说“假期”,然后使用选择器getElementsByClassName()querySelector()将它们全部放入数组中。

Once you've done that, you loop through this array of elements and you disable them when the user choose to disable via the checkbox element. 完成此操作后,您将遍历此元素数组,并在用户选择通过复选框元素禁用时禁用它们。

 const mySelectElements = document.getElementsByClassName("holiday"); const holidayCheck = document.getElementById("holiday_check"); function closed_holiday(){ for(let selectElement of mySelectElements){ if(holidayCheck.checked){ selectElement.setAttribute('disabled', 'disabled'); } else{ selectElement.removeAttribute('disabled'); } } } 
 <div> <span>Opening Time:</span> <select class="holiday" id="holiday_time_h1"><option>00</option></select> : <select class="holiday" id="holiday_time_m1"><option>00</option></select> <span>Closing time</span> <select class="holiday" id="holiday_time_h2"><option>00</option></select> : <select class="holiday" id="holiday_time_m2"><option>00</option></select> <input id="holiday_check" class="check" type="checkbox" onchange="closed_holiday();">Closed</input> </div> 

Note that this uses jquery's $.each to iterate over the arrays. 请注意,这使用jquery的$.each遍历数组。

Also, here I am using wildcards to select all elements that begin with id holiday_check and then looping over them and disabling them. 另外,在这里我使用通配符来选择所有以id holiday_check开头的元素,然后遍历它们并禁用它们。

 function closed_holiday() { if (document.getElementById('holiday_check').checked) { var checkboxes = document.querySelectorAll('[id^=holiday_time]'); //Jquery Solution //$.each(checkboxes, function() { // this.setAttribute('disabled', 'disabled'); //}); for (i=0; i<checkboxes.length; i++){ checkboxes[i].setAttribute('disabled', 'disabled'); } } else { var checkboxes = document.querySelectorAll('[id^=holiday_time]'); //Jquery solution //$.each(checkboxes, function() { // this.removeAttribute('disabled'); //}); for (i=0; i<checkboxes.length; i++){ checkboxes[i].removeAttribute('disabled'); } } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <span>Opening Time:</span> <select id="holiday_time_h1"> <option>00</option> </select> : <select id="holiday_time_m1"> <option>00</option> </select> <span>Closing time</span> <select id="holiday_time_h2"> <option>00</option> </select> : <select id="holiday_time_m2"> <option>00</option> </select> <input id="holiday_check" class="check" type="checkbox" onchange="closed_holiday();">Closed</input> </div> 

Since you're looking to disable multiple <select> elements at a time, applying a class name to each of them greatly simplifies DOM lookups in your event handler. 由于您要一次禁用多个<select>元素,因此将类名应用于每个元素可以极大地简化事件处理程序中的DOM查找。 Note that instead of manipulating the HTML attribute disabled you can toggle the disabled property, and this is much terser! 请注意,您可以切换disabled属性,而不是操作HTML属性disabled ,这非常麻烦! Finally, consider attaching an event listener to your checkbox in JS as opposed to writing callbacks in HTML. 最后,考虑将事件侦听器附加到JS中的复选框,而不是用HTML编写回调。

 var selects = document.getElementsByClassName('holiday-select'), checkbox = document.getElementById('holiday_check') function closed_holiday() { for (var i = 0; i < selects.length; i++) { selects[i].disabled ^= true // toggle } } if (checkbox.checked) { closed_holiday() } checkbox.addEventListener('change', closed_holiday) 
 <div> <span>Opening Time:</span> <select id="holiday_time_h1" class="holiday-select"><option>00</option></select> : <select id="holiday_time_m1" class="holiday-select"><option>00</option></select> <span>Closing time</span> <select id="holiday_time_h2" class="holiday-select"><option>00</option></select> : <select id="holiday_time_m2" class="holiday-select"><option>00</option></select> <input id="holiday_check" class="check" type="checkbox">Closed</input> </div> 

 function closed_holiday() { if(document.getElementById('holiday_check').checked) { document.querySelectorAll('select').forEach((function(x){ x.setAttribute('disabled', 'disabled');})) } else { document.querySelectorAll('select').forEach((function(x){ x.removeAttribute('disabled');})); } } 
 <div> <span>Opening Time:</span> <select id="holiday_time_h1"><option>00</option></select> : <select id="holiday_time_m1"><option>00</option></select> <span>Closing time</span> <select id="holiday_time_h2"><option>00</option></select> : <select id="holiday_time_m2"><option>00</option></select> <input id="holiday_check" class="check" type="checkbox" onchange="closed_holiday();">Closed </div> 

You can do easily disable and enable by just one line, 您只需一行即可轻松禁用和启用,

 if (document.getElementById('holiday_check').checked) document.getElementById('holiday_time_h1').setAttribute('disabled', 'disabled'); $('#holiday_check').on('change', function() { if (document.getElementById('holiday_check').checked) { document.getElementById('holiday_time_h1').setAttribute('disabled', 'disabled'); $('.Timesheet select').attr('disabled', 'disabled'); //ADDED LINE } else { document.getElementById('holiday_time_h1').removeAttribute('disabled'); $('.Timesheet select').removeAttr('disabled'); //ADDED LINE } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="Timesheet"> <span>Opening Time:</span> <select id="holiday_time_h1"> <option>00</option> </select> : <select id="holiday_time_m1"> <option>00</option> </select> <span>Closing time</span> <select id="holiday_time_h2"> <option>00</option> </select> : <select id="holiday_time_m2"> <option>00</option> </select> <input id="holiday_check" class="check" type="checkbox">Closed</input> </div> 

No need of extra code 不需要额外的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM