當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。