zoukankan      html  css  js  c++  java
  • 使用js设置input标签只读 readonly 属性

    先上代码:

    复制代码
    <html>
    
    <head>
        <title> test </title>
        <meta charset="utf-8">
    </head>
    <script language="JavaScript">
        function setReadonly() {
            //document.getElementById("test").setAttribute("readOnly", true);
            document.getElementById("test").readOnly=true;
        }
    
        function readWrite() {
            //document.getElementById("test").setAttribute("readOnly", false);
            document.getElementById("test").readOnly=false;
        }
        
    </script>
    
    <body>
        <form name="addform" id="addform" method="post" action="">
            <input type="text" id="test" >
            <input type="button"  value="只读" onclick="setReadonly();">
            <input type="button"  value="读写" onclick="readWrite();">
        </form>
    </body>
    
    </html>
    复制代码

      代码挺简单的,但是这一点代码也有可能达不到预期效果。

      一开始,在readWrite()函数中,我用的是注释掉的那些代码,没有用红色部分代码,结果点击“读写”按钮后,还是不可编辑。于是就改成了红色部分代码,结果再点击“读写”按钮就可以编辑了。

      总结了一下,当达不到预期效果时,可以使用以下几种解决方案:

      1、readonly中 将‘o’改为大写‘O’ ,即将readonly改为readOnly ;

      2、将上述代码中的setReadonly()和readWrite()函数中设置readOnly读写属性的那些代码组合着全试一遍,基本就能达到预期效果了。

  • 相关阅读:
    VSFTPD匿名用户上传文件
    shell随机数比较
    Verse For Santa ---- CodeForces
    计算机基础--整数加减运算
    Hello,Blog!
    题解 P2486 【[SDOI2011]染色】
    题解 P1047 【校门外的树】
    题解 P1339 【[USACO09OCT]热浪Heat Wave】——线段树做法
    题解 P1197 【[JSOI2008]星球大战】
    题解 P1886 【滑动窗口】
  • 原文地址:https://www.cnblogs.com/yanzi-meng/p/8352590.html
Copyright © 2011-2022 走看看