这是我的功能
function toggleCheckbox (element) {
if(document.getElementById("checkbox").checked = true) {
document.getElementById("strAPISuccessURL").value = "http://www.gladstonebrookes.co.uk/thank-you/";
}
else {
document.getElementById("strAPISuccessURL").value = "http://www.gladstonebrookes.co.uk/the-call/";
}
}
并且该函数在复选框 onClick 事件上调用
<input type="checkbox" name="checkbox" id="checkbox" onchange="toggleCheckbox(this)" />
表单输入字段的默认值如下
<input type="hidden" name="strAPISuccessURL" id="strAPISuccessURL" value="http://www.gladstonebrookes.co.uk/the-call/" />
当我单击该复选框时,id="strAPISuccessURL"的输入值会按预期更改。我遇到的问题是,我无法“取消选中”该框,以便 URL 恢复为原始状态。
预先感谢您的任何建议
最佳答案
您在 if
中使用了单个 =
。这是将值设置为 true
,然后返回 true
,而不是“检查”它是否为 true
并返回。
您很可能想要使用===
if (document.getElementById("checkbox").checked === true) {
// ...
}
关于javascript - 将 Javascript onClick 事件添加到 HTML 复选框阻止了我 'unchecking' 复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21730586/