我在标签内有一个链接。问题是,当用户在阅读条款后单击“后退”时,复选框未选中,因为当他们单击链接时,他们同时也取消选中该框,因为链接位于标签内。
<input type="checkbox" id="terms" name="terms" checked="checked" />
<label for="terms">I agree to be bound by the <a href="/terms">Terms</a></label>
单击链接时如何防止复选框被选中?尝试在点击标签时执行 event.preventDefault()
,但这并不能阻止复选框被选中/取消选中。
我可以直接从标签中取出链接(这意味着更多的 CSS 样式)。但现在我很好奇上述是否可能。
最佳答案
您可以通过将其路由到 onclick 事件来取消点击事件。
“返回错误;”部分将阻止点击事件向上移动到标签。
<input type="checkbox" id="terms" name="terms" checked="checked" />
<label for="terms">I agree to be bound by the <a href="#" onclick="window.open('/terms','_blank');return false;">Terms</a></label>
关于javascript - 单击标签内的链接时如何防止复选框选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1098269/