HTML DOM 输入复选框 name 属性
htmlweb developmentfront end technology更新于 2025/6/14 9:16:17
HTML DOM 输入复选框 name 属性返回一个字符串,该字符串是输入复选框 name 属性的值。用户也可以将其设置为新的字符串。
语法
以下是语法 −
- 返回字符串值
inputCheckboxObject.name
- 将 name 属性设置为字符串值
inputCheckboxObject.name = ‘String’
示例
让我们看一个输入复选框 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>
输出
这将产生以下输出 −
勾选"启用新名称"复选框之前 −

勾选"启用新名称"复选框之后 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

