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读写属性的那些代码组合着全试一遍,基本就能达到预期效果了。

  • 相关阅读:
    PHP设计模式之工厂模式
    ThinkPHP删除栏目(多)
    斐波纳契数列递归和非递归算法
    单链表反转的实现
    找出n个数中最大的k个数
    实验四:掌握Linux系统的构建和调试方法
    npm如何上传自己的包
    简要谈一下部署时候的操作
    sass的基本语法及使用
    this 指向问题
  • 原文地址:https://www.cnblogs.com/yanzi-meng/p/8352590.html
Copyright © 2011-2022 走看看