checkbox 復選框無法設置選中與否狀態


最近寫關於表格內部復選框點擊選中切換,研究半個小時沒研究出來,最后快放棄的時候發現竟然因為jquery 版本問題,1.6以后的版本用attr()設置是無效的必須用prop()屬性設置。如果想點擊切換復選框狀態,代碼如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-1.11.3.js"></script>
    <style>
        td{
            padding: 15px;
            border: 1px solid red;
        }
    </style>
</head>
<body>
<table>
    <tr>
        <td><input type="checkbox"  ></td>
        <td><input type="checkbox" ></td>
        <td><input type="checkbox" ></td>
        <td><input type="checkbox" ></td>
        <td><input type="checkbox" ></td>
    </tr>
</table>
<script>
    $(function(){
        $("td").on('click',function(){
            if($(this).find("input").is(":checked")){
                $(this).find("input").removeAttr("checked")
            }else{
                $(this).find("input").prop("checked","checked")
            }
        })
    })
</script>
</body>
</html>

效果如下:

 


免責聲明!

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



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