HTML DOM Input Checkbox name 屬性


HTML DOM Input Checkbox name 屬性返回一個字串,它是 input checkbox 的 name 屬性的值。使用者還可以將其設定為一個新的字串。

語法

以下是語法 −

  • 返回字串值
inputCheckboxObject.name
  • name 屬性設定為字串值
inputCheckboxObject.name = ‘String’

示例

讓我們看一個 Input Checkbox name 屬性的示例 −

 線上演示

<!DOCTYPE html>
<html>
<head>
<title>Name Attribute of Checkbox</title>
</head>
<body>
<form id="Form">
<div>
Enable new name: <input id="formID" type="checkbox" name="formID">
</div>
</form>
<button onclick="getFormID()">Change Name Attribute</button>
<div id="nameAttr"></div>
<script>
   function getFormID(){
      var oldNameAttr = document.getElementById("formID");
      var newNameAttr = document.getElementById("nameAttr");
      if(oldNameAttr.checked == true){
         oldNameAttr.name = 'newFormID';
         newNameAttr.textContent = 'Updated value of name attribute: '+oldNameAttr.name;
      } else {
         newNameAttr.textContent = 'Check the checkbox'+ ', current value of name attribute: '+oldNameAttr.name ;
      }
   }
</script>
</body>
</html>

輸出

這將產生以下輸出 −

在勾選“啟用新名稱”複選框之前 −

勾選“啟用新名稱”複選框之後 −

更新於: 2019 年 7 月 30 日

124 次檢視

開啟您的 職業生涯

完成課程即可獲得認證

開始
廣告