2014-02-12 69 views
0

這裏是我的功能添加一個JavaScript onClick事件到HTML複選框已阻止我「取消選中」複選框

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恢復爲原始狀態。

在此先感謝您的任何建議

回答

2

你在你的if使用單一=。這是將值設置爲true,然後返回true而不是「檢查」,如果它是true並返回。

您很可能希望使用===

if (document.getElementById("checkbox").checked === true) { 
    // ... 
} 
+0

這是一個漫長的一天!不能相信我錯過了,感謝您的快速回復!現在一切正常,因爲它應該:-) –

0

您已經使用=代替==,你也可以試試這個,

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/"; 
} 
相關問題