HTML DOM Input Radio 型別的屬性


HTML DOM Input radio 型別的屬性與 type=”radio” 的 input 元素相關聯。它始終會為 input radio 元素返回 radio。

語法

以下是 radio 型別屬性的語法 −

radioObject.type

示例

讓我們看一個 radio 型別屬性的示例 −

 現場演示

<!DOCTYPE html>
<html>
<body>
<h1>Input radio type Property</h1>
<form>
FRUIT:
<input type="radio" name="fruits" id="Mango">Mango
<br>
</form>
<p>Get the above input element type by clicking the below button</p>
<button type="button" onclick="radioType()">GET Type</button>
<p id="Sample"></p>
<script>
   function radioType() {
      var P=document.getElementById("Mango").type;
      document.getElementById("Sample").innerHTML = "The type for the input field is: "+P ;
   }
</script>
</body>
</html>

輸出

這將生成以下輸出 −

單擊型別按鈕 GET −

我們首先在 form 中建立了一個 type=”radio” 的 input 元素,name=”fruits”,id=”Mango” −

<form>
FRUIT:
<input type="radio" name="fruits" id="Mango">Mango
</form>

然後,我們建立了“GET 型別”按鈕,使用者單擊後將執行 radioType() 方法 −

<button type=”button” onclick="radioType()">GET Type</button>

radioType() 方法使用 getElementById() 方法獲取 input 元素,並將它的 type 屬性值分配給變數 P。然後使用 innerHTML 屬性,將這個變數顯示在 id 為“Sample”的段落中 −

function getType() {
   var P = document.getElementById("Mango").type;
   document.getElementById("Sample").innerHTML = "The type for the input field is : "+P;
}

更新日期: 2019 年 8 月 22 日

115 次瀏覽

開啟您的 職業生涯

完成課程後獲得認證

開始學習
廣告