HTML DOM 輸入 Radio 表單屬性


HTML DOM Input Radio form 屬性用於返回包含給定輸入單選按鈕的表單引用。如果單選按鈕位於表單之外,它只會返回 NULL。此屬性為只讀。

語法

以下是輸入 radio form 屬性的語法 -

radioObject.form

示例

讓我們看一個 Input radio form 屬性的示例。

<!DOCTYPE html>
<html>
<body>
<h1>Input radio form Property</h1>
<form id="FORM1">
FRUIT:
<input type="radio" name="fruits" id="Mango">Mango
</form>
<p>Get the form id by clicking on the below button</p>
<button type="button" onclick="formId()">GET FORM</button>
<p id="Sample"></p>
<script>
   function formId() {
      var P=document.getElementById("Mango").form.id;
      document.getElementById("Sample").innerHTML = "The id of the form containing the radio
      button is: "+P ;
   }
</script>
</body>
</html>

輸出

這會產生以下輸出 -

單擊 GET FORM 按鈕後 -

在上面的示例中 -

我們首先在 type=”radio”、name=”fruits”、id=”Mango” 的 form 中建立了一個輸入元素。該 form 的 id 屬性值設定為 ”FORM1” -

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

然後我們建立了一個按鈕 GET FORM,當用戶單擊它時,該按鈕將執行 formId() 方法 -

<button type="button" onclick="formId()">GET FORM</button>

formId() 方法使用 getElementById() 方法獲取 type=radio 的輸入欄位,獲取包含單選按鈕的 form 物件的 id 屬性。然後它將此值分配給變數 P,並使用其 innerHTML 屬性將其顯示在 id“Sample”的段落中 -

function formId() {
   var P=document.getElementById("Mango").form.id;
   document.getElementById("Sample").innerHTML = "The id of the form containing the password field is: "+P ;
}

更新時間:21-Aug-2019

88 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告