使用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM