我在用户输入数据并提交后禁用输入字段。但是,如果我们刷新或按ctl + F5,禁用的字段将启用并变为可编辑状态。下面是我的代码,即使刷新,我该怎么做才能让它被禁用?谢谢。
<script type="text/javascript">
document.getElementById('myButton').onclick = function() {
document.getElementById('firstname').disabled = true;
document.getElementById('lastname').disabled = true;
document.getElementById('email').disabled = true;
};
</script>
我使用localstorage来存储数据
答案 0 :(得分:6)
最简单的解决方案是使用sessionStorage:
<script type="text/javascript">
const disableInputs = function() {
sessionStorage.disableInputs = 'true';
document.getElementById('firstname').disabled = true;
document.getElementById('lastname').disabled = true;
document.getElementById('email').disabled = true;
};
if (sessionStorage.disableInputs === 'true') disableInputs();
document.getElementById('myButton').onclick = disableInputs;
</script>
如果您希望禁用效果持续超过会话,请改用localStorage
。
答案 1 :(得分:1)
使用sessionStorage:
<script type="text/javascript">
var disable = function() {
document.getElementById('firstname').disabled = true;
document.getElementById('lastname').disabled = true;
document.getElementById('email').disabled = true;
sessionStorage.setItem("isDisabled", true);
};
document.getElementById('myButton').onclick = disable;
if(sessionStorage.getItem("isDisable") === 'true') {
disable();//call your function
}
</script>
答案 2 :(得分:-2)
将设置置于
中COOKIE版
function disablefield(){
document.getElementById('firstname').disabled = true;
document.getElementById('lastname').disabled = true;
document.getElementById('email').disabled = true;
}
document.getElementById('myButton').onclick = function() {
$.cookie("is_disable", 1);
disablefield();
};
if ($.cookie("is_disable")==1) disablefield();
</script>
会话版
function disablefield(){
document.getElementById('firstname').disabled = true;
document.getElementById('lastname').disabled = true;
document.getElementById('email').disabled = true;
}
document.getElementById('myButton').onclick = function() {
$.session.set("is_disable", "1");
disablefield();
};
if ($.session.get("is_disable")==1) disablefield();
</script>