diff --git a/assets/javascripts/app.js b/assets/javascripts/app.js index b04f714..8c47fc8 100644 --- a/assets/javascripts/app.js +++ b/assets/javascripts/app.js @@ -3482,7 +3482,10 @@ $(function() { var $li = $(this); var labelText = $li.find('label').text().trim(); - // ✅ li 保留 tabindex="0",但補上正確的 aria-label + // ✅ 明確補上 tabindex="0",原始 HTML 並沒有這個屬性,不能只靠註解假設它存在 + $li.attr('tabindex', '0'); + + // ✅ 補上正確的 aria-label $li.attr('aria-label', labelText + ',按下 Enter 或空白鍵選取'); // ✅ checkbox 設為 tabindex="-1",讓焦點只停在 li 一次 @@ -3495,27 +3498,6 @@ $(function() { var isChecked = $li.find('input[type="checkbox"]').prop('checked'); $li.attr('aria-checked', isChecked ? 'true' : 'false'); }); - - // ✅ li 的 Enter/Space 觸發 click - $('.check-item').off('keydown.a11y').on('keydown.a11y', function(e) { - if (e.which === 13 || e.which === 32) { - e.preventDefault(); - var label = this.querySelector('label'); - if (label) { - label.click(); // 直接點 label,瀏覽器會自動連動勾選 checkbox,click 事件也會正常往上冒泡 - } - } - }); - - // ✅ click 後同步 aria-checked 狀態 - $('.check-item').off('click.a11y').on('click.a11y', function() { - var $li = $(this); - var isChecked = $li.find('input[type="checkbox"]').prop('checked'); - $li.attr('aria-checked', isChecked ? 'true' : 'false'); - // 同步更新其他 li 的 aria-checked - $('.check-item').not($li).attr('aria-checked', 'false'); - }); - }, 800); } @@ -3524,6 +3506,26 @@ $(function() { $(window).on('resize', function() { fixCheckItemA11y(); }); + + // ✅ 改用委派綁在 document 上:不管 .check-item 是非同步才出現、 + // 還是後來被 reorganize_roles_ul() 整批重新產生,都不會失效 + $(document).off('keydown.checkItemA11y').on('keydown.checkItemA11y', '.check-item', function(e) { + if (e.which === 13 || e.which === 32) { + e.preventDefault(); + var label = this.querySelector('label'); + if (label) { + label.click(); // 直接點 label,瀏覽器會自動連動勾選 checkbox,click 事件也會正常往上冒泡 + } + } + }); + + // ✅ click 後同步 aria-checked 狀態(同樣改用委派,避免被其他地方的 .off("click") 意外清掉) + $(document).off('click.checkItemA11y').on('click.checkItemA11y', '.check-item', function() { + var $li = $(this); + var isChecked = $li.find('input[type="checkbox"]').prop('checked'); + $li.attr('aria-checked', isChecked ? 'true' : 'false'); + $('.check-item').not($li).attr('aria-checked', 'false'); + }); });