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


HTML Input Radio name用法及代码示例


HTML DOM中的DOM输入单选名称属性用于设置或返回单选按钮的name属性的值。每个输入字段都需要name属性。如果未在输入字段中指定name属性,则将根本不发送该字段的数据。

用法:

  • 它返回输入无线电名称属性。
    radioObject.name
  • 它用于设置输入无线电名称属性。
    radioObject.name = name

属性值:它包含一个值名称,该值定义单选按钮的名称。


返回值:它返回一个表示单选按钮名称的字符串值。

示例1:本示例说明了如何返回输入单选名称属性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <style> 
        body { 
            text-align:center; 
        } 
          
        h1 { 
            color:green; 
        } 
    </style> 
</head> 
  
<body> 
    <h1> 
      GeeksforGeeks 
  </h1> 
  
    <h2> 
      HTML DOM Input Radio name Property 
  </h2>  
  Radio Button:
    <input type="radio" 
           checked=true 
           id="radioID"
           value="Geeks_radio" 
           name="Geek_radio"> 
    <br> 
    <br> 
    <button onclick="GFG()"> 
        Click! 
    </button> 
    <p id="GFG"
       style="font-size:25px; 
              color:green;"> 
  </p> 
    <script> 
        function GFG() { 
  
            // Accessing input element  
            // type="radio"  
            var x = 
                document.getElementById( 
                  "radioID").name; 
            
            document.getElementById( 
              "GFG").innerHTML = x; 
        } 
    </script> 
  
</body> 
  
</html>

输出:
在单击按钮之前:

单击按钮后:

示例2:本示例说明了如何设置属性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <style> 
        body { 
            text-align:center; 
        } 
          
        h1 { 
            color:green; 
        } 
    </style> 
</head> 
  
<body> 
    <h1> 
      GeeksforGeeks 
  </h1> 
  
    <h2> 
      HTML DOM Input Radio name Property 
  </h2>  
  Radio Button:
    <input type="radio"
           checked=true 
           id="radioID"
           value="Geeks_radio"
           name="Geek_radio"> 
    <br> 
    <br> 
    <button onclick="GFG()"> 
        Click! 
    </button> 
    <p id="GFG"
       style="font-size:25px; 
              color:green;"> 
  </p> 
    <script> 
        function GFG() { 
  
            // Accessing input element  
            // type="radio"  
            var x = 
                document.getElementById( 
                  "radioID").name = "myGeeks"; 
            
            document.getElementById( 
              "GFG").innerHTML =  
              "The value of the name attribute"+ 
              " was changed to " + x; 
        } 
    </script> 
  
</body> 
  
</html>

输出:

在单击按钮之前:

单击按钮后:

支持的浏览器:DOM输入单选名称属性支持的浏览器如下:

  • 谷歌浏览器
  • Internet Explorer 10.0以上
  • Firefox
  • Opera
  • Safari


相关用法


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