提交按钮加载相同的页面,禁用按钮

问题描述:

我的代码在单击按钮后加载相同的页面,但我希望禁用按钮加载页面。下面是java脚本代码。提交按钮加载相同的页面,禁用按钮

function disableSendVerifButton() 
{ 
    $('.sendVerificationButton').html('Verification Sent'); 
    $('.sendVerificationButton').attr("disabled", true); 

    // Change the button color 
    $('.sendVerificationButton').addClass('gray'); 
} 

但是,由于加载了相同的页面,按钮不会被禁用并且按下正常的按钮加载。请帮忙。

+0

您需要以某种方式存储按钮的状态。如果您使用的是HTML5,则可以使用localStorage,否则,您也可以将状态存储在$ _SESSION中。原因:在页面重新加载时,整个JS被重新初始化 – 2014-09-02 06:29:11

+0

假设你的页面两次都是相同的,你需要在页面上改变一些东西 - 无论是从后端脚本,还是在查询字符串中(你可以在js)或cookie。你需要纯粹在JavaScript中做到这一点? – Jason 2014-09-02 06:31:31

+0

@ moo2u2:不,它并不是硬性规则,只能使用java脚本。欢迎您提出建议以使用其他方法。还有一件事是,一旦我离开该页面并再次返回,该按钮应该再次启用。 – 2014-09-02 06:34:46

你可以设置一个查询字符串上重新加载页面上/提交,f.ex:

http://domain.com/?submitted 

然后看它在javascript:

if (/^\?submitted/.test(window.location.search)) { 
    disableSendVerifButton(); 
} 

使用Cookie:DO点击这里提交/点击刷新:

document.cookie='submitted=1;' 

然后:

if (/submitted\=1;/.test(document.cookie)) { 
    document.cookie='submitted=; expires=Thu, 01 Jan 1970 00:00:01 GMT;' 
    disableSendVerifButton(); 
} 

其他选项的localStorage或通过Ajax提交。

+0

Cookie有什么选择?你能解释一下吗? – 2014-09-02 06:38:28

+1

@arpit_s添加了一个简单的Cookie示例。但我会去查询字符串。 – David 2014-09-02 07:44:26