将 Javascript onClick 事件添加到 HTML 复选框已阻止我'unchecking'该复选框



这是我的函数

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) {
    // ...
}

你用了=代替==,你也可以试试这个,

if(element.checked) {
   document.getElementById("strAPISuccessURL").value = "http://www.gladstonebrookes.co.uk/thank-you/";
}else{
  document.getElementById("strAPISuccessURL").value = "http://www.gladstonebrookes.co.uk/the-call/";
}

相关内容

最新更新