2013-07-29 141 views
5

我的登录页面中有一个提交按钮。我想在点击一次后禁用提交按钮。我的工作正常,查询this.disabled = true;,禁用按钮后,它会进入下一页。问题是,当我按下返回按钮时,它仍然是禁用的,因为页面不从服务器重新加载,它从本地缓存加载。每次当我回到登录页面时,我想启用按钮。我可以清除缓存,但是会出现一些问题,例如是否会从服务器加载,项目会很慢。什么是最好的方式来做到这一点?单击问题后禁用按钮

<script> 
$(document).ready(function(){ 
    $(document).on('click','button',function(){ 
     this.disabled = true 
     window.location="https://www.google.co.in/" 
    }) 
}) 
</script> 
<body> 
<button type="submit" id="button">Click Me!</button> 
</body> 
+0

为什么你需要禁用它,如果它去到另一个页面? – Sergio

+0

@Sergio - 可能会避免多个表单提交或某种程度上。 – sbeliv01

+0

@ sbeliv01,是的。只是好奇想更好地理解OP的想法。 – Sergio

回答

2

我的按钮初始disabled属性设置为在体内HTML残疾证明代码不会重新启用它(这样可以去掉,在体内) 我希望这有助于:

<script> 
$(document).ready(function() { 
$("#button").removeAttr('disabled'); 
$("#button").click(function(){ 
     $(this).attr('disabled','disabled'); 
     window.location="https://www.google.co.in/" 
    }); 
}); 
</script> 
<body> 
<button type="submit" id="button" disabled="disabled">Click Me!</button> 
</body> 
+0

@StevelKB它在浏览器中工作正常,不在android。 – Nullify

+0

你介意在你的问题中加入android标签,谢谢! – SteveKB

2

您需要另一个确保在页面加载时启用按钮的事件。有点像:

<script> 
$(document).ready(function(){ 
    $("#button").disabled = false; 
    $(document).on('click','button',function(){ 
     this.disabled = true 
     window.location="https://www.google.co.in/" 
    }) 
}) 
</script> 
<body> 
<button type="submit" id="button">Click Me!</button> 
</body> 

我没有尝试运行这个,所以我不知道如果语法是100%,但它应该给你正确的想法。

+0

这将禁用该按钮,然后点击启用它。 – mikeswright49

+1

否。onclick监听器中的disable设置为true。 Document.ready()在渲染页面后调用。此代码启用页面呈现后的按钮。当然,我不确定如果从缓存中加载.ready()将被调用。 – retrohacker

+0

你应该仔细阅读问题仔细.button启用将启动时,它会触发$(“#button”)。disabled = false;点击后退按钮不会触发任何查询,它会从缓存中返回页面 – Nullify