使用js控制readonly、disabled属性

参考

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function switchUser() {
            let usernameEl = document.getElementById("username");
            const beforeVal = usernameEl.getAttribute("readonly");
            console.log("切换前状态:" + getStatus(beforeVal));
            if (beforeVal === null) {
                usernameEl.setAttribute("readonly", "readonly");
            } else {
                usernameEl.removeAttribute("readonly");
            }
            const afterVal = usernameEl.getAttribute("readonly");
            console.log("切换后状态:" + getStatus(afterVal));
        }
 
        function getStatus(readonlyVal) {
            if (readonlyVal === null) {
                return "读写"
            } else {
                return "只读"
            }
        }
    </script>
</head>
<body>
 
用户名:<input type="text" id="username" name="username"><br>
<button onclick="switchUser()">切换用户名readonly属性</button>
 
</body>
</html>

disabled (同readonly)

var rowNum = RBResultGrid.mulLineCount; // MulLine.js
if (BusinessType=="02" || BusinessType=="03") {
    for (var i = 0; i < rowNum; i++) {
        document.getElementsByName("RBResultGrid15")[i].setAttribute("disabled",true);
        document.getElementsByName("RBResultGrid16")[i].setAttribute("disabled",true);
    }
}else {
    for (var i = 0; i < rowNum; i++) {
        document.getElementsByName("RBResultGrid15")[i].removeAttribute("disabled");
        document.getElementsByName("RBResultGrid16")[i].removeAttribute("disabled");
    }
}

使用jquery

使用$("#id").attr("readonly","true"); 添加readonly属性

使用$("#id").removeAttr("readonly"); 移除该属性

【参考】https://blog.csdn.net/weixin_38405770/article/details/77847602

上一篇:*官方信誉平台注册邀请码怎么填写99999930


下一篇:元素节点的获取操作