diff --git a/assets/javascripts/app.js b/assets/javascripts/app.js index dae93f8..d2bce52 100644 --- a/assets/javascripts/app.js +++ b/assets/javascripts/app.js @@ -3483,5 +3483,38 @@ setTimeout(function() { } }); }, 700); +$(function() { + function fixLabelForMismatch() { + setTimeout(function() { + // ✅ 找所有 label,檢查 for 屬性是否有對應的 input id + $('label[for]').each(function() { + var $label = $(this); + var forAttr = $label.attr('for'); + + // ✅ 已有正確對應則跳過 + if ($('#' + forAttr).length > 0) return; + + // ✅ 找同一個 control-group 裡的 input/select/textarea + var $group = $label.closest('.control-group'); + if ($group.length === 0) return; + + var $input = $group.find('input:not([type="hidden"]):not([type="submit"]):not([type="button"]):not([type="reset"]), select, textarea').first(); + if ($input.length === 0) return; + + var inputId = $input.attr('id'); + if (!inputId) { + // ✅ input 沒有 id,補上一個和 for 一致的 id + $input.attr('id', forAttr); + } else { + // ✅ input 有 id 但和 for 不一致,把 label for 改成 input id + $label.attr('for', inputId); + } + }); + + }, 500); + } + + fixLabelForMismatch(); +}); }(jQuery, window));