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


HTML Input Checkbox name用法及代碼示例


HTML DOM中的“輸入複選框名稱”屬性用於設置或返回輸入複選框字段的名稱屬性的值。每個輸入字段都需要name屬性。如果未在輸入字段中指定name屬性,則將根本不發送該字段的數據。

用法:

  • 它返回輸入複選框名稱屬性。
    checkboxObject.name
  • 它用於設置“輸入複選框”名稱屬性。
    checkboxObject.name = name
  • 屬性值:它包含單個屬性值名稱,該名稱用於指定複選框的名稱。


    返回值:它返回一個字符串值,該字符串值表示輸入複選框字段的名稱。

    範例1:本示例返回輸入複選框名稱屬性。

    <!DOCTYPE html>  
    <html>  
        <head>  
            <title> 
                DOM Input Checkbox name Property 
            </title>  
        </head> 
          
        <body style = "text-align:center;">  
          
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1>  
              
            <h2>DOM Input Checkbox name Property</h2> 
              
            <form >  
              
                <!-- Below input elements have attribute "checked" -->
                <input type="checkbox" name="check" id="GFG"
                        value="1" checked>Checked by default<br>  
                          
                <input type="checkbox" name="check" value="2"> 
                        Not checked by default<br>  
            </form> <br> 
              
            <button onclick="myGeeks()"> 
                Submit 
            </button> 
              
            <p id="sudo" style="color:green;font-size:25px;"></p> 
              
            <!-- Script to set Input Checkbox name Property -->
            <script> 
                function myGeeks() { 
                    var g = document.getElementById("GFG").name 
                    document.getElementById("sudo").innerHTML = g; 
                } 
            </script> 
        </body>  
    </html>                               

    輸出:
    在單擊按鈕之前:

    單擊按鈕後:

    範例2:本示例設置“輸入複選框”名稱屬性。

    <!DOCTYPE html>  
    <html>  
        <head>  
            <title> 
                DOM Input Checkbox name Property 
            </title>  
        </head>  
          
        <body style = "text-align:center;">  
          
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1>  
              
            <h2>DOM Input Checkbox name Property</h2>  
              
            <form >  
              
                <!-- Below input elements have attribute 
                    "checked" -->
                <input type="checkbox" name="check" id="GFG"
                        value="1" checked>Checked by default<br>  
                          
                <input type="checkbox" name="check" value="2"> 
                        Not checked by default<br>  
            </form> <br> 
              
            <button onclick="myGeeks()"> 
                Submit 
            </button> 
              
            <p id="sudo" style="color:green;font-size:20px;"></p> 
              
            <!-- Script to set Input Checkbox name property -->
            <script> 
                function myGeeks() { 
                    var g = document.getElementById("GFG").name 
                            = "uncheck"; 
                    document.getElementById("sudo").innerHTML 
                            = "The value of the name attribute" 
                              + " was changed to " + g; 
                } 
            </script> 
        </body>  
    </html>                     

    輸出:
    在單擊按鈕之前:

    單擊按鈕後:

    支持的瀏覽器:下麵列出了DOM輸入支持的瀏覽器複選框名稱屬性:

    • 穀歌瀏覽器
    • IE瀏覽器
    • Firefox
    • Opera
    • Safari


相關用法


注:本文由純淨天空篩選整理自ManasChhabra2大神的英文原創作品 HTML | DOM Input Checkbox name Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。