HTML DOM 輸入重置表單屬性


HTML DOM 輸入重置表單屬性用於返回包含給定重置按鈕的表單引用。如果重置按鈕位於表單外部,它將僅返回 NULL。此屬性只讀。

語法

以下是輸入重置表單屬性的語法。

resetObject.form

示例

我們來看看輸入重置表單屬性的一個示例 -

 現場演示

<!DOCTYPE html>
<html>
<body>
<h1>Input reset form Property</h1>
<form id="FORM_1" style="border:solid 2px green;padding:2px">
UserName: <input type="text" id="USR"> <br>
Location: <input type="text" id="Age"> <br><br>
<input type="reset" id="RESET1">
</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("RESET1").form.id;
      document.getElementById("Sample").innerHTML = "The id of the form containing the reset button is: "+P ;
   }
</script>
</body>
</html>

這將產生以下輸出 -

單擊獲取表單按鈕 -

在上面的示例中 -

我們建立了一個帶有 type=”reset” 和 id=”RESET1” 的 <input> 元素。單擊此按鈕將重置表單資料。此按鈕位於包含兩個文字欄位並對其應用內聯樣式的表單中。

<form style="border:solid 2px green;padding:2px">
UserName: <input type="text" id="USR"> <br>
Location: <input type="text" id="Age"> <br><br>
<input type="reset" id="RESET1">
</form>

然後,我們建立了一個按鈕獲取表單,它將在使用者單擊時執行 formId() 方法。

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

formId() 方法使用 getElementById() 方法來獲取型別為 reset 的輸入欄位,並獲取包含重置按鈕的表單物件的 id 屬性。然後,將此值分配給變數 P,並使用其 innerHTML 屬性顯示在 id 為“Sample”的段落中 −

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

更新於:2019-08-22

180 次瀏覽

啟動你的 職業

完成課程即可獲得認證

開始學習
廣告