根据选中单选按钮显示/隐藏div的account功能异常求解
问题原因排查
你的account函数无法运行完全是语法拼写错误和API使用错误导致,具体问题如下:
- 大小写错误:原生DOM方法是
document.getElementById(),你全部写成了getElementByID(最后一个d大写,不符合JS大小写敏感的规则) - 拼写错误:
documen.getElementByID少写了字母t,正确拼写为document - API不存在:没有
document.getElementsByID这个方法,getElementById一次只能查询单个ID的元素,不能同时传多个ID参数 - 逻辑顺序错误:单选按钮是互斥选中状态,你的代码三个判断条件并行执行,选中
bothcheck时前面两个判断会先把ifc和ifo设为隐藏,逻辑冲突
修复后的account函数代码
function account() { // 提前缓存元素避免重复查询DOM const ifcEl = document.getElementById('ifc'); const ifoEl = document.getElementById('ifo'); const cCheck = document.getElementById('ccheck'); const oCheck = document.getElementById('ocheck'); const bothCheck = document.getElementById('bothcheck'); if (cCheck.checked) { ifcEl.style.display = 'block'; ifoEl.style.display = 'none'; } else if (oCheck.checked) { ifcEl.style.display = 'none'; ifoEl.style.display = 'block'; } else if (bothCheck.checked) { ifcEl.style.display = 'block'; ifoEl.style.display = 'block'; } }
额外优化建议
你页面里两组货币单选按钮用了同一个name="currency",会导致两组单选互斥,建议把ifo容器内的货币单选的name属性改成currencyop,避免选C组货币时O组的选中状态被取消。
内容的提问来源于stack exchange,提问作者user17432629
相关产品推荐
相关产品推荐

