GubE 2020-03-06 19:00 采纳率: 91.7%
浏览 201
已采纳

js怎么动态重复添加checkbox?

怎么实现每次实现addCb函数都能添加一次?且遍历星期1-7

<body>
    <form action=" " method="post" id="fa">
       <!-- <input type="checkbox" name="Cb1" value="a">星期1<br/>-->
    </form>
    <input type="button" onclick="addCb()" value="addCb" >
<script>
    let oCheckbox=document.createElement("input");
    let myText=document.createTextNode("星期1");
    oCheckbox.setAttribute("type","checkbox");
    oCheckbox.setAttribute("id","mayi");
    function addCb() {
        let fa=document.getElementById("fa");
        fa.appendChild(oCheckbox);
        fa.appendChild(myText);
    }

</script>
</body>
  • 写回答

1条回答 默认 最新

  • 李国庆❀ 2020-03-06 20:49
    关注
    <body>
        <form action=" " method="post" id="fa">
           <!-- <input type="checkbox" name="Cb1" value="a">星期1<br/>-->
        </form>
        <input type="button" onclick="addCb()" value="addCb" >
        <script>
            let oCheckbox=document.createElement("input");
            let myText=document.createTextNode("星期1");
            oCheckbox.setAttribute("type","checkbox");
            oCheckbox.setAttribute("id","mayi");
            function addCb() {
                let fa=document.getElementById("fa");
                fa.appendChild(oCheckbox);
                fa.appendChild(myText);
                body = document.getElementsByTagName("body");
                body.appendChild(fa);
            }
    
        </script>
    </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?