[英]Clicking on label not checking checkbox if hidden when using IE 7 or 8
[英]javascript confirm when checking a checkbox by clicking on the label
我的表單上有一個復選框,它做一些危險的事情。 因此,我想確保用戶在確定該項目時確實很確定,但是如果他們取消選中此復選框,則我不想警告他們。
我的問題是,如果他們單擊實際的復選框以取消選中它,而不是標簽的文本,則可以正常工作。
function askApply() { if (document.getElementById("apply").checked) { var answer = confirm("Are you sure about that?"); if (!answer) { document.getElementById("apply").checked = false; } } }
<form> <label onclick="askApply();"> <input type="checkbox" name="apply" id="apply" value="1" /> Apply </label> </form>
一些注意事項:
change
事件,而不要click
。 例如,可以使用鍵盤更改復選框。 addEventListener
。 document.getElementById('apply').addEventListener('change', function() { if(this.checked) { var answer = confirm("Are you sure about that?"); if (!answer) { document.getElementById("apply").checked = false; } } });
<form> <label> <input type="checkbox" name="apply" id="apply" value="1" /> Apply </label> </form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.