Input Text name 属性
获取文本域的名称:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<script>
function displayResult() {
var x = document.getElementById("email").name;
alert(x);
}
</script>
</head>
<body>
<form>
Email: <input type="text" id="email" name="email">
</form>
<button type="button" onclick="displayResult()">显示名称</button>
</body>
</html>
定义和用法
name 属性设置或返回文本域的名称。
name 属性用于对提交到服务器后的表单数据进行标识,或者在客户端通过 JavaScript 引用表单数据。
注意: 只有设置了 name 属性的表单元素才能在提交表单时传递它们的值。
浏览器支持
![]()
![]()
![]()
![]()
![]()
所有主要浏览器都支持 name 属性。
语法
返回 name 属性:
textObject.name
设置 name 属性:
textObject.name=name
Property Values
| 值 | 描述 |
|---|---|
| name | 规定了文本域的名称 |
技术详情
| 返回值: | 字符串,表示文本域的名称 |
|---|
更多实例
修改文本域的名称:
用户名: <input type="text" id="myText" name="usrnm">
<p>点击按钮显示或修改文本域 name 属性的值。</p>
<button onclick="display()">显示名称</button>
<button onclick="change()">修改名称</button>
<script>
function display() {
var x = document.getElementById("myText").name;
alert("文本域的名称为:" + x);
}
function change() {
var x = document.getElementById("myText").name = "username";
alert("名称修改为:" + x);
}
</script>
相关页面
HTML 参考手册: HTML <input> name 属性
Input Text 对象
Input Text 对象