如何通过单击一次取消选中所有复选框?

huangapple go评论110阅读模式
英文:

How to uncheck all checkboxes with a single click?

问题

我用JavaScript编写了一个用于使用<kbd>SHIFT</kbd>键选择复选框的功能。现在我想要通过单击来取消选择。

HTML:

  1. <div class="inbox">
  2. <div class="item">
  3. <input type="checkbox" />
  4. <p>This is an inbox layout.</p>
  5. </div>
  6. <div class="item">
  7. <input type="checkbox" />
  8. <p>Check one item</p>
  9. </div>
  10. <div class="item">
  11. <input type="checkbox" />
  12. <p>Hold down your Shift key</p>
  13. </div>
  14. <div class="item">
  15. <input type="checkbox" />
  16. <p>Check a lower item</p>
  17. </div>
  18. <div class="item">
  19. <input type="checkbox" />
  20. <p>Everything in between should also be set to checked</p>
  21. </div>
  22. <div class="item">
  23. <input type="checkbox" />
  24. <p>Try do it without any libraries</p>
  25. </div>
  26. </div>

JavaScript 用于选择复选框:

  1. const checkboxes = document.querySelectorAll(
  2. '.inbox input[type="checkbox"]'
  3. );
  4. let lastChecked;
  5. function handleCheck(e) {
  6. // 用于选择复选框
  7. let inBetween = false;
  8. // 检查是否按下了Shift键
  9. // 并检查是否正在勾选它
  10. if (e.shiftKey && this.checked) {
  11. // 继续执行我们的操作
  12. // 遍历每个复选框
  13. checkboxes.forEach(checkbox => {
  14. console.log(checkbox);
  15. if (checkbox === this || checkbox === lastChecked) {
  16. inBetween = !inBetween;
  17. console.log('Starting to check them in between!');
  18. }
  19. if (inBetween) {
  20. checkbox.checked = true;
  21. }
  22. });
  23. }
  24. lastChecked = this;
  25. }
  26. checkboxes.forEach(checkbox =>
  27. checkbox.addEventListener('click', handleCheck)
  28. );

我希望在使用<kbd>SHIFT</kbd>键选择后,当我单击已选择的复选框时,后面的已选择复选框将会取消选择,只需单击一次。

英文:

I made this in JavaScript for selecting checkboxes using the <kbd>SHIFT</kbd> key. Now I want to deselect it by a single click.

HTML:

  1. &lt;div class=&quot;inbox&quot;&gt;
  2. &lt;div class=&quot;item&quot;&gt;
  3. &lt;input type=&quot;checkbox&quot; /&gt;
  4. &lt;p&gt;This is an inbox layout.&lt;/p&gt;
  5. &lt;/div&gt;
  6. &lt;div class=&quot;item&quot;&gt;
  7. &lt;input type=&quot;checkbox&quot; /&gt;
  8. &lt;p&gt;Check one item&lt;/p&gt;
  9. &lt;/div&gt;
  10. &lt;div class=&quot;item&quot;&gt;
  11. &lt;input type=&quot;checkbox&quot; /&gt;
  12. &lt;p&gt;Hold down your Shift key&lt;/p&gt;
  13. &lt;/div&gt;
  14. &lt;div class=&quot;item&quot;&gt;
  15. &lt;input type=&quot;checkbox&quot; /&gt;
  16. &lt;p&gt;Check a lower item&lt;/p&gt;
  17. &lt;/div&gt;
  18. &lt;div class=&quot;item&quot;&gt;
  19. &lt;input type=&quot;checkbox&quot; /&gt;
  20. &lt;p&gt;Everything in between should also be set to checked&lt;/p&gt;
  21. &lt;/div&gt;
  22. &lt;div class=&quot;item&quot;&gt;
  23. &lt;input type=&quot;checkbox&quot; /&gt;
  24. &lt;p&gt;Try do it without any libraries&lt;/p&gt;
  25. &lt;/div&gt;
  26. &lt;/div&gt;

JavaScript to select the checkbox:

  1. const checkboxes = document.querySelectorAll(
  2. &#39;.inbox input[type=&quot;checkbox&quot;]&#39;
  3. );
  4. let lastChecked;
  5. function handleCheck(e) {
  6. //for selecting the checkboxes
  7. let inBetween = false;
  8. // Check if they had the shift key down
  9. // AND check that they are checking it
  10. if (e.shiftKey &amp;&amp; this.checked) {
  11. // go ahead and do what we please
  12. // loop over every single checkbox
  13. checkboxes.forEach(checkbox =&gt; {
  14. console.log(checkbox);
  15. if (checkbox === this || checkbox === lastChecked) {
  16. inBetween = !inBetween;
  17. console.log(&#39;Starting to check them in between!&#39;);
  18. }
  19. if (inBetween) {
  20. checkbox.checked = true;
  21. }
  22. });
  23. }
  24. lastChecked = this;
  25. }
  26. checkboxes.forEach(checkbox =&gt;
  27. checkbox.addEventListener(&#39;click&#39;, handleCheck)
  28. );

I want after selecting with <kbd>SHIFT</kbd> key, when I click on a selected checkbox the selected checkboxes which comes after that to be unchecked with a single click.

答案1

得分: 0

你可以通过将复选框放入一个数组中并获取要检查的复选框范围来简化你的逻辑:

  1. // 将所有复选框收集到一个数组中以便更容易处理
  2. const checkboxes = [...document.querySelectorAll('.inbox input[type="checkbox"])];
  3. let lastChecked;
  4. function handleCheck(e) {
  5. if (e.shiftKey) {
  6. // 我们应该将范围内的所有复选框都设置为这个值(包括最后选中的)
  7. const checked = lastChecked?.checked;
  8. // 获取要检查的复选框范围,从lastChecked到this(包括两者)
  9. const range = [lastChecked, this].map(check => checkboxes.indexOf(check)).sort((a, b) => a - b);
  10. // 获取范围内的复选框并将其选中
  11. checkboxes.slice(range[0], range[1] + 1).forEach(check => check.checked = checked);
  12. }
  13. lastChecked = this;
  14. }
  15. checkboxes.forEach(checkbox =>
  16. checkbox.addEventListener('click', handleCheck)
  17. );
  1. p {
  2. display: inline-block;
  3. margin: 0;
  4. }
  1. <div class="inbox">
  2. <div class="item">
  3. <input type="checkbox" />
  4. <p>This is an inbox layout.</p>
  5. </div>
  6. <div class="item">
  7. <input type="checkbox" />
  8. <p>Check one item</p>
  9. </div>
  10. <div class="item">
  11. <input type="checkbox" />
  12. <p>Hold down your Shift key</p>
  13. </div>
  14. <div class="item">
  15. <input type="checkbox" />
  16. <p>Check a lower item</p>
  17. </div>
  18. <div class="item">
  19. <input type="checkbox" />
  20. <p>Everything in between should also be set to checked</p>
  21. </div>
  22. <div class="item">
  23. <input type="checkbox" />
  24. <p>Try do it without any libraries</p>
  25. </div>
  26. </div>
英文:

You could simplify your logic by putting your checkboxes into an array and getting the range of checkboxes to check:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. // collect all the checkboxes into an array for easier handling
  2. const checkboxes = [...document.querySelectorAll(&#39;.inbox input[type=&quot;checkbox&quot;]&#39;)];
  3. let lastChecked;
  4. function handleCheck(e) {
  5. if (e.shiftKey) {
  6. // we should set all checkboxes in the range with this value (including the last checked)
  7. const checked = lastChecked?.checked;
  8. // get the range of checkboxes to check between lastChecked and this (including both)
  9. const range = [lastChecked, this].map(check =&gt; checkboxes.indexOf(check)).sort((a, b) =&gt; a - b);
  10. // get checkboxes in the range and check them
  11. checkboxes.slice(range[0], range[1] + 1).forEach(check =&gt; check.checked = checked);
  12. }
  13. lastChecked = this;
  14. }
  15. checkboxes.forEach(checkbox =&gt;
  16. checkbox.addEventListener(&#39;click&#39;, handleCheck)
  17. );

<!-- language: lang-css -->

  1. p{
  2. display:inline-block;
  3. margin:0;
  4. }

<!-- language: lang-html -->

  1. &lt;div class=&quot;inbox&quot;&gt;
  2. &lt;div class=&quot;item&quot;&gt;
  3. &lt;input type=&quot;checkbox&quot; /&gt;
  4. &lt;p&gt;This is an inbox layout.&lt;/p&gt;
  5. &lt;/div&gt;
  6. &lt;div class=&quot;item&quot;&gt;
  7. &lt;input type=&quot;checkbox&quot; /&gt;
  8. &lt;p&gt;Check one item&lt;/p&gt;
  9. &lt;/div&gt;
  10. &lt;div class=&quot;item&quot;&gt;
  11. &lt;input type=&quot;checkbox&quot; /&gt;
  12. &lt;p&gt;Hold down your Shift key&lt;/p&gt;
  13. &lt;/div&gt;
  14. &lt;div class=&quot;item&quot;&gt;
  15. &lt;input type=&quot;checkbox&quot; /&gt;
  16. &lt;p&gt;Check a lower item&lt;/p&gt;
  17. &lt;/div&gt;
  18. &lt;div class=&quot;item&quot;&gt;
  19. &lt;input type=&quot;checkbox&quot; /&gt;
  20. &lt;p&gt;Everything in between should also be set to checked&lt;/p&gt;
  21. &lt;/div&gt;
  22. &lt;div class=&quot;item&quot;&gt;
  23. &lt;input type=&quot;checkbox&quot; /&gt;
  24. &lt;p&gt;Try do it without any libraries&lt;/p&gt;
  25. &lt;/div&gt;
  26. &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

你可以将这部分添加到你的 handleCheck 函数中:

  1. else if (e.shiftKey && !this.checked) {
  2. // 如果按下了 Shift 键并且正在取消选中复选框
  3. // 遍历每个复选框并取消中间的复选框选中状态
  4. checkboxes.forEach(checkbox => {
  5. if (checkbox === this || checkbox === lastChecked) {
  6. inBetween = !inBetween;
  7. }
  8. if (inBetween) {
  9. checkbox.checked = false;
  10. }
  11. });
  12. }
英文:

You can add this to your handleCheck :

  1. else if (e.shiftKey &amp;&amp; !this.checked) {
  2. // If shift key is down and checkbox is being unchecked
  3. // loop over every single checkbox and deselect the ones in between
  4. checkboxes.forEach(checkbox =&gt; {
  5. if (checkbox === this || checkbox === lastChecked) {
  6. inBetween = !inBetween;
  7. }
  8. if (inBetween) {
  9. checkbox.checked = false;
  10. }
  11. });
  12. }

答案3

得分: 0

似乎您没有检查文档是否准备好。

我只是将您的代码包装在DOMContentLoaded事件监听器中,它可以正常工作。

  1. document.addEventListener("DOMContentLoaded", () => {
  2. const checkboxes = document.querySelectorAll(
  3. '.inbox input[type="checkbox"]'
  4. );
  5. let lastChecked;
  6. function handleCheck(e) {
  7. // 用于选择复选框
  8. let inBetween = false;
  9. // 检查是否按下了Shift键
  10. // 并检查是否正在检查它
  11. if (e.shiftKey && this.checked) {
  12. // 继续执行我们想要的操作
  13. // 循环遍历每个复选框
  14. checkboxes.forEach(checkbox => {
  15. console.log(checkbox);
  16. if (checkbox === this || checkbox === lastChecked) {
  17. inBetween = !inBetween;
  18. console.log('Starting to check them in between!');
  19. }
  20. if (inBetween) {
  21. checkbox.checked = true;
  22. }
  23. });
  24. }
  25. lastChecked = this;
  26. }
  27. checkboxes.forEach(checkbox =>
  28. checkbox.addEventListener('click', handleCheck)
  29. );
  30. });

如您所见,没有更多要添加的内容。
希望这能帮助到您。

英文:

it seems you do not check the document is ready.

I just wrapped your code into a DOMContentLoaded Eventlistener and it works.

  1. document.addEventListener(&quot;DOMContentLoaded&quot;, () =&gt; {
  2. const checkboxes = document.querySelectorAll(
  3. &#39;.inbox input[type=&quot;checkbox&quot;]&#39;
  4. );
  5. let lastChecked;
  6. function handleCheck(e) {
  7. //for selecting the checkboxes
  8. let inBetween = false;
  9. // Check if they had the shift key down
  10. // AND check that they are checking it
  11. if (e.shiftKey &amp;&amp; this.checked) {
  12. // go ahead and do what we please
  13. // loop over every single checkbox
  14. checkboxes.forEach(checkbox =&gt; {
  15. console.log(checkbox);
  16. if (checkbox === this || checkbox === lastChecked) {
  17. inBetween = !inBetween;
  18. console.log(&#39;Starting to check them in between!&#39;);
  19. }
  20. if (inBetween) {
  21. checkbox.checked = true;
  22. }
  23. });
  24. }
  25. lastChecked = this;
  26. }
  27. checkboxes.forEach(checkbox =&gt;
  28. checkbox.addEventListener(&#39;click&#39;, handleCheck)
  29. );
  30. });

As you cann see there isn't more to add.
Hope this will help.

答案4

得分: -1

通过对您的代码进行一些更改,这将实现取消选中和选中的功能。它会停在最后一个已选中(或取消选中)的位置,与您的原始示例相同,我假设这是您想要的行为。

  1. const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]');
  2. let lastChecked;
  3. function handleCheck(e) {
  4. // 用于选择复选框
  5. let inBetween = false;
  6. // 检查是否按下了Shift键
  7. // 并检查是否正在选中它
  8. if (e.shiftKey/* && this.checked */) { // 忽略当前的选中状态
  9. // 继续执行我们希望的操作
  10. // 遍历每个复选框
  11. checkboxes.forEach((checkbox) => {
  12. console.log(checkbox);
  13. if (checkbox === this || checkbox === lastChecked) {
  14. inBetween = !inBetween;
  15. console.log("开始选中它们之间的复选框!");
  16. }
  17. if (inBetween) {
  18. checkbox.checked = this.checked; // 处理选中/取消选中
  19. }
  20. });
  21. }
  22. lastChecked = this;
  23. }
  24. checkboxes.forEach((checkbox) =>
  25. checkbox.addEventListener("click", handleCheck)
  26. );

注意:上面的代码用于处理复选框的选择和取消选择操作,它允许使用Shift键来一次性选择多个复选框。

英文:

With a couple changes to your code, this should accomplish unchecking as well as checking. It will stop at the lastChecked (or unchecked) as in your original example, I assumed that's the behavior you wanted.

  1. const checkboxes = document.querySelectorAll(&#39;.inbox input[type=&quot;checkbox&quot;]&#39;);
  2. let lastChecked;
  3. function handleCheck(e) {
  4. //for selecting the checkboxes
  5. let inBetween = false;
  6. // Check if they had the shift key down
  7. // AND check that they are checking it
  8. if (e.shiftKey/* &amp;&amp; this.checked */) { // ignore current checked state
  9. // go ahead and do what we please
  10. // loop over every single checkbox
  11. checkboxes.forEach((checkbox) =&gt; {
  12. console.log(checkbox);
  13. if (checkbox === this || checkbox === lastChecked) {
  14. inBetween = !inBetween;
  15. console.log(&quot;Starting to check them in between!&quot;);
  16. }
  17. if (inBetween) {
  18. checkbox.checked = this.checked; // handles both checked/unchecked
  19. }
  20. });
  21. }
  22. lastChecked = this;
  23. }
  24. checkboxes.forEach((checkbox) =&gt;
  25. checkbox.addEventListener(&quot;click&quot;, handleCheck)
  26. );

huangapple
  • 本文由 发表于 2023年7月20日 16:05:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定