2012-04-19 51 views
0

我有一個php窗體和一個名爲countdown.js的單獨的JavaScript文件,它運行到一個目標日期的計時器,然後顯示一條消息,當完成時,要讓提交按鈕禁用。當定時器在JavaScript文件上運行時禁用單獨的php文件上的提交按鈕

這是我的形式

<form id="MakeBid" action="MakeBid.php" method="POST"> 
<input type="hidden" name="propertyID" value ="1"/> 
<div>Bid Now <input type="text" name="pricesoldfor"/></div> 
<input id = "submit" input type="submit" value="Submit" /> 
</form> 

這是我的Java代碼

function calcage(secs, num1, num2) { 
    s = ((Math.floor(secs/num1)) % num2).toString(); 
    if (LeadingZero && s.length < 2) s = "0" + s; 
    return "<b>" + s + "</b>"; 
} 

function CountBack(secs) { 
    if (secs < 0) { 
     document.getElementById("cntdwn").innerHTML = FinishMessage; 


     return; 
    } 
    DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs, 86400, 100000)); 
    DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs, 3600, 24)); 
    DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs, 60, 60)); 
    DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs, 1, 60)); 
    document.getElementById("cntdwn").innerHTML = DisplayStr; 
    if (CountActive) setTimeout("CountBack(" + (secs + CountStepper) + ")", SetTimeOutPeriod); 
} 

function putspan(backcolor, forecolor) { 
    document.write("<span id='cntdwn' style='background-color:" + backcolor + "; color:" + forecolor + "'></span>"); 
} 

if (typeof(BackColor) == "undefined") BackColor = "white"; 
if (typeof(ForeColor) == "undefined") ForeColor = "black"; 
if (typeof(TargetDate) == "undefined") TargetDate = "12/31/2020 5:00 AM"; 
if (typeof(DisplayFormat) == "undefined") DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds."; 
if (typeof(CountActive) == "undefined") CountActive = true; 
if (typeof(FinishMessage) == "undefined") FinishMessage = ""; 
if (typeof(CountStepper) != "number") CountStepper = -1; 
if (typeof(LeadingZero) == "undefined") LeadingZero = true; 
CountStepper = Math.ceil(CountStepper); 
if (CountStepper == 0) CountActive = false; 
var SetTimeOutPeriod = (Math.abs(CountStepper) - 1) * 1000 + 990; 
putspan(BackColor, ForeColor); 
var dthen = new Date(TargetDate); 
var dnow = new Date(); 
if (CountStepper > 0) ddiff = new Date(dnow - dthen); 
else ddiff = new Date(dthen - dnow); 
gsecs = Math.floor(ddiff.valueOf()/1000); 
CountBack(gsecs);​ 
+0

**永不**將字符串傳遞給'setInterval()'或'setTimeout()'。這樣做與使用'eval()'一樣糟糕,並且只要使用變量,就會導致不可讀和可能不安全的代碼,因爲您需要將它們插入到字符串中,而不是傳遞實際變量。正確的解決方案是'setInterval(function(){/ * your code *)},msecs);'。 'setTimeout()'同樣適用。如果你只想調用一個沒有任何參數的函數,你也可以直接傳遞函數名:'setInterval(someFunction,msecs);'(注意函數名後面有** no **'()') – ThiefMaster 2012-04-19 10:45:03

回答

0

我preassumed你的計時器運作良好,消息顯示正確。

您是否嘗試過使用jQuery?要禁用提交按鈕,只需插入以下行:

$("input[type=submit]").attr("disabled", "disabled"); 

這是您想實現的目標嗎?

而且下面我貼你有類似的問題解決鏈接:

jQuery disable/enable submit button

編輯:

根據你的代碼(並假設它的工作),請執行下列操作:

  • 將jQuery添加到您的網站:http://jquery.com/
  • 修改您的CountBack f添加我在document.getElementById("cntdwn").innerHTML = FinishMessage;之前提供的代碼return;

應該工作!

+1

甚至更​​好,'.prop('disabled',true)' – ThiefMaster 2012-04-19 10:44:50

+0

卡洛斯會我把那行在我的JavaScript代碼? – Jobbo05 2012-04-19 12:20:12

+0

我編輯了我的答案 – Karol 2012-04-19 13:05:25

相關問題