当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML Input Checkbox用法及代码示例


HTML中的输入复选框对象表示HTML表单中的复选框。

对于HTML表单中<input type = “checkbox”>元素的每个实例,将创建一个复选框对象。要访问复选框对象,请为相应形式的元素数组建立索引或使用getElementById();

创建复选框对象:我们可以通过JavaScript创建复选框对象。要创建<input type = “checkbox”>元素,请使用document.createElement()方法。创建后,使用appendChild()方法将其附加到特定元素(例如div)上以显示它。


例:

<!DOCTYPE html> 
<html> 
    <head> 
        <title> 
            DOM Input Checkbox Property 
        </title> 
    </head> 
    <body style = "text-align:center;"> 
        <h1 style = "color:green;"> 
            GeeksforGeeks 
        </h1> 
        <h2> 
            DOM Input Checkbox Property 
        </h2> 
        <p>Click the button to create a checkbox.</p> 
        <button onclick="geek()">Click me!</button> 
        <br> 
        <div id = "myDiv"></div> 
        <script> 
        function geek() { 
            var myDiv = document.getElementById("myDiv"); 
              
            // creating checkbox element 
            var checkbox = document.createElement('input'); 
              
            // Assigning the attributes 
            // to created checkbox 
            checkbox.type = "checkbox"; 
            checkbox.name = "name"; 
            checkbox.value = "value"; 
            checkbox.id = "id"; 
              
            // creating label for checkbox 
            var label = document.createElement('label'); 
              
            // assigning attributes for  
            // the created label tag  
            label.htmlFor = "id"; 
              
            // appending the created text to  
            // the created label tag  
            label.appendChild(document.createTextNode('This is the 
                              label for checkbox.')); 
              
            // appending the checkbox 
            // and label to div 
            myDiv.appendChild(checkbox); 
            myDiv.appendChild(label); 
        } 
        </script> 
    </body> 
</html>

输出:
在单击按钮之前:
comcheckbox
单击按钮后:
domcheckbox

访问复选框对象:我们可以使用getElementById()方法访问该复选框对象。将复选框元素的ID放入getElementById()中以对其进行访问。
例:

<!DOCTYPE html> 
<html> 
    <head> 
        <title> 
            DOM Input Checkbox Property 
        </title> 
    </head> 
    <body style = "text-align:center;"> 
        <h1 style = "color:green;"> 
            GeeksforGeeks 
        </h1> 
        <h2> 
            DOM Input Checkbox Property 
        </h2> 
        <p>Click the button to check the checkbox.</p> 
  
        <button onclick="myFunction()">Click me!</button> 
        <br> 
        Checkbox:<input type="checkbox" id="check"> 
        <script> 
            function myFunction() { 
  
              // fetching the checkbox by id 
              var doc = document.getElementById("check"); 
                
              // changing the state of checkbox to checked 
              doc.checked = true; 
            } 
        </script> 
    </body> 
</html>

输出:
在单击按钮之前:
domcheck
单击按钮后:
domcheck



相关用法


注:本文由纯净天空筛选整理自Vishal Chaudhary 2大神的英文原创作品 HTML | DOM Input Checkbox Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。