jQuery禁用表单元素

5

情景是我写了一个自动刷新表单元素的函数,并且我希望在登录按钮上添加onclick事件,以禁用自动刷新功能。

我无法禁用自动刷新,页面仍然会刷新。

我的代码如下:

$('#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);,脚本中会出现多少错误啊? - Val
2
实际上,应该是 setInterval(timerIncrement, 15000); - eval 是邪恶的! - Jonas Høgh
@anu 这是一个非常常见的问题,使用 prop 而不是 attr 就可以解决。 - noob
1个回答

1

我猜你需要 清除 定时器:

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

@Nitinvarpe 已修复!谢谢! - falsarella

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接