以下代碼在Chrome和IE中有效,但在Firefox中不起作用。腳本部分在Chrome和IE中工作,但不在Firefox中
這個想法是強制用戶在推進之前檢查「同意」框,方法是遵循可用的可用鏈接之一。
<script type="text/javascript">
function agreeCheck()
{
valid = false;
var agree = document.getElementById('agree');
if(isAgree(agree)){
valid= true;
}
return valid;
}
function isAgree(elem)
{
if (elem.checked == false)
{
Show_Stuff(agreespan, "true");
return false;
}
else
{
Show_Stuff(agreespan, "false");
return true;
}
}
function Show_Stuff(warning,on_off)
// Function that will swap the display/no display for
// all content within span tags
{
if ((warning.style.display == "none")&&(on_off =="true"))
{
warning.style.display = "";
}
else
{
warning.style.display = "none";
}
}
</script>
<input type="checkbox" name="agree" value="signed" id="agree">
I agree</input> <span ID="agreespan" style="display: none">
<font color="red">You must agree in order to proceed</font>
</span>
<button type="button" title="Proceedt" class="btn-proceed" onclick="if (agreeCheck()==true){ window.location='myURL'; } else{ return agreeCheck();}"></button>
<input type="image" src="anotherURL" title="myTitle" onClick="return agreeCheck();"/>
注:
- 明顯myURL和anotherURL是 實際有效的URL
- 上點擊時未選中該複選框的第一個按鈕阻止頁面的進展,但沒有透露錯誤信息在Chrome和IE的範圍內。在Firefox中,不管框狀態如何,它都不會執行任何操作。
- 當未選中該框時,單擊圖像鏈接(輸入類型=「圖像」)可在Chrome和IE中正常工作,並顯示錯誤消息。在Firefox中,無論盒子的狀態如何,都會遵循鏈接。
- 我意識到這可以用不同的寫法來簡化事情。問題是我在Magento中實現了這個功能,我只能分別訪問代碼塊,因此我不能將If Else語句的各個部分組合到一個單獨的函數中。
非常感謝您的幫助!
**編輯:我改變了if語句(最後一行之前),以
if (agree.checked ==true){ ...
這個固定在Chrome和IE,現在這些瀏覽器是否正常運行的問題。 Firefox仍然沒有做我想做的事
關閉我的頭頂,你可以是具有與試圖直接通過其ID訪問「agreespan」 span標籤的麻煩。我不知道這是否是跨瀏覽器兼容的。在訪問「同意」複選框時,您應該考慮使用「document.getElemenyById」,就像您在agreeCheck函數中所做的一樣。 – WesleyJohnson 2011-06-07 21:54:56
一些無關的語法問題:你忘記了用var語句聲明'valid',因爲它是一個隱含的全局語句。你甚至不需要'valid',如果它超過if,你可以在if或者'false'裏面返回'true'。不要將''true''和''false''作爲字符串傳遞,而應該使用'true'和'false'原語。 – 2011-06-07 21:57:27
你的文檔是什麼? – Knu 2011-06-27 19:45:19