简体   繁体   English

页面重新加载后启用了禁用的文本框

[英]Disabled text box is enabled after page reload

I have a simple js validation function that checks if a checkbox is checked, if it's then the textbox input is enabled for the user, but when the checkbox is not checked it automatically makes the textbox field disabled. 我有一个简单的js验证功能,用于检查是否选中了复选框,如果选中,则为用户启用了文本框输入,但是当未选中复选框时,它会自动禁用文本框字段。

The problem is that when after saving the page in the AJAX with not checked field, causes that the textbox field is again enabled even the checkbox is not checked , when I check it again 2 times then the function works again, but each time the page reloads and save previously selected values the function does not works at is should. 问题是,在将页面保存在AJAX中且未选中字段之后,导致即使未选中复选框也再次启用了文本框字段,当我再次选中2次时,该功能再次起作用,但是每次页面应该重新加载并保存该功能不起作用的先前选择的值。

What I am doing wrong? 我做错了什么? Is there a different way to prevent this behavior? 是否有其他方法可以防止这种行为?

function enableTextBodField() {

 var checkboxField= document.querySelector('.checkbox');
 var textBoxField= document.querySelector('.textBoxField');

 if (checkboxField.checked == false)
 {
    textBoxField.disabled = true;
 }
 else if (checkboxField.checked == true)
 {
    textBoxField.disabled = false;
 }
}

You can store the state of that textbox in browser localStorage and work it out from there. 您可以将该文本框的状态存储在浏览器localStorage中,然后从那里进行计算。

$(document).ready(function() {
    var textboxState = localStorage.getItem("txtboxState"); // Get state from localstorage
    var textBoxField= document.querySelector('.textBoxField');
    var checkboxField= document.querySelector('.checkbox');

    if(textboxState != "" || textboxState != NULL){
      if(textboxState = "hidden"){
       textBoxField.disabled = true;
       checkboxField.checked = false;
      }else{
        if(textboxState == "visible"){
          textBoxField.disabled = false;
          checkboxField.checked = true;
        }
      }
    }else{
       textBoxField.disabled = false;
       checkboxField.checked = false;
    }

});

function enableTextBodField() {
 var checkboxField= document.querySelector('.checkbox');
 var textBoxField= document.querySelector('.textBoxField');

 if (checkboxField.checked == false)
 {
    textBoxField.disabled = true;
    localStorage.setItem("txtboxState","hidden"); // Set state in localstorage variable
 }
 else if (checkboxField.checked == true)
 {
    textBoxField.disabled = false;
    localStorage.setItem("txtboxState","visible"); // Set state in localstorage variable
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM