2012-01-17 94 views
5

情景是我寫了一個表單元素自動刷新的功能,我想登錄按鈕上的onclick事件來禁用自動刷新功能。jquery禁用表單元素

我無法獲得禁用autorefresh和頁面仍然刷新。

我的代碼是:

$('#form').ready(function() { //Increment the idle time counter every minute. 
    var idleInterval = setInterval("timerIncrement()", 15000); // 1 minute 

    //Zero the idle timer on mouse movement. 
    $(this).mousemove(function (e) { 
     idleTime = 0; 
    }); 
    $(this).keypress(function (e){ 
     idleTime = 0; 
    }); 
}); 

function timerIncrement() { 
    idleTime = idleTime + 1; 
    if (idleTime > 2) { // 20 minutes 
     window.location.reload(); 
    } 
} 

$('#login').ready(function() { 

    alert("working promptly"); 
    //Zero the idle timer on mouse movement. 
    $(this).click(function (e) { 
     alert("Hi In click !"); 
     $('#form').attr("disabled","true"); 
    }); 


}); 
+1

'的setInterval( 「timerIncrement()」,15000);'應該是' setInterval(「timerIncrement」,15000);'和omg,你可以在腳本中做多少錯誤? – Val 2012-01-17 10:35:47

+2

其實它應該是'setInterval(timerIncrement,15000);' - eval是邪惡的! – 2012-01-17 10:39:53

+0

@anu這是一個非常普遍的問題,使用'prop'而不是'attr',它會起作用。 – noob 2012-01-17 11:42:26

回答

1

我想你需要到明確間隔定時器:

$(this).click(function (e) { 
    alert("Hi In click !"); 
    $('#form').attr("disabled","true"); 
    clearInterval(idleInterval); 
}); 
+1

我應該是clearInterval(idleInterval); – 2013-12-05 12:09:17

+0

@Nitinvarpe修正!謝謝! – falsarella 2013-12-05 12:12:03