gpt4 book ai didi

javascript - 如果选中复选框,则只允许单击链接

转载 作者:行者123 更新时间:2023-11-30 10:32:20 25 4
gpt4 key购买 nike

我正在尝试设置一个按钮/链接,该按钮/链接只有在选中复选框时才可单击。所以到目前为止我的代码是

<form>
<input type="checkbox" name="agreeCheckbox" value="agreeCheckbox">By clicking this you agree that you are adding a subscription/recurring product to your order<br>
</form>

<a href="exmaple.com">This link is only clickable if checkbox is checked</a>

我假设我必须在 javascript 中执行此操作,尽管我是 javascript 的初学者。谢谢

最佳答案

此代码向元素添加了一些id 属性,以便为Javascript 提供一些钩子(Hook)。在单击复选框之前,它会隐藏并阻止 anchor 的默认操作。

HTML

<form>
<input id="agreement" type="checkbox" name="agreeCheckbox" value="agreeCheckbox">By clicking this you agree that you are adding a subscription/recurring product to your order<br>
</form>

<a href="exmaple.com" id="link">This link is only clickable if checkbox is checked</a>

Javascript

var chk = document.getElementById("agreement");
var anchor = document.getElementById("link");
anchor.style.display = "none";
anchor.onclick = function(e){
e.preventDefault();
}

chk.onclick = function(){
if(chk.checked){
anchor.style.display = "inline";
anchor.onclick = "";
}
}

工作示例

http://jsfiddle.net/zVCD7/

关于javascript - 如果选中复选框,则只允许单击链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16243705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com