当使用HTML <input>标记时,使用名称和id属性之间的区别是什么,特别是我发现它们有时命名相同?
当前回答
名称定义了表单提交后属性的名称。因此,如果您稍后想要读取此属性,您将在POST或GET请求中的“name”下找到它。
而在JavaScript或CSS中,id用于寻址字段或元素。
其他回答
我希望下面的例子对你有帮助:
<!DOCTYPE html>
<html>
<head>
<script>
function checkGender(){
if(document.getElementById('male').checked) {
alert("Selected gender: "+document.getElementById('male').value)
}else if(document.getElementById('female').checked) {
alert("Selected gender: "+document.getElementById('female').value)
}
else{
alert("Please choose your gender")
}
}
</script>
</head>
<body>
<h1>Select your gender:</h1>
<form>
<input type="radio" id="male" name="gender" value="male">Male<br>
<input type="radio" id="female" name="gender" value="female">Female<br>
<button onclick="checkGender()">Check gender</button>
</form>
</body>
</html>
在代码中,请注意两个'name'属性是相同的,以定义'male'或'female'之间的可选性,但'id'不等于以区分它们。
name属性用于发布到例如web服务器。id主要用于CSS(和JavaScript)。假设你有这样的设置:
<input id="message_id" name="message_name" type="text" />
为了在发布表单时使用PHP获取该值,它将使用name属性,如下所示:
$_POST["message_name"];
如前所述,当您想使用特定的CSS内容时,id用于样式化。
#message_id
{
background-color: #cccccc;
}
当然,您可以对id和name属性使用相同的名称。这两者不会互相干扰。
此外,name可以用于更多的项目,比如当你使用单选按钮时。然后使用Name对单选按钮进行分组,因此只能选择其中一个选项。
<input id="button_1" type="radio" name="option" />
<input id="button_2" type="radio" name="option" />
在这个非常具体的例子中,我可以进一步说明id是如何使用的,因为你可能需要一个带有单选按钮的标签。Label有一个for属性,它使用输入的id将这个标签链接到输入(单击标签时,选中按钮)。下面是一个例子
<input id="button_1" type="radio" name="option" /><label for="button_1">Text for button 1</label>
<input id="button_2" type="radio" name="option" /><label for="button_2">Text for button 2</label>
name用于在DOM(文档对象模型)中提交表单。
ID用于DOM中HTML控件的唯一名称,尤其是JavaScript和CSS控件。
在HTML4.01:
名称属性
Valid only on <a>, <form>, <iframe>, <img>, <map>, <input>, <select>, <textarea> Name does not have to be unique, and can be used to group elements together such as radio buttons & checkboxes Can not be referenced in URL, although as JavaScript and PHP can see the URL there are workarounds Is referenced in JavaScript with getElementsByName() Shares the same namespace as the id attribute Must begin with a letter According to specifications is case sensitive, but most modern browsers don't seem to follow this Used on form elements to submit information. Only input tags with a name attribute are submitted to the server
Id属性
Valid on any element except <base>, <html>, <head>, <meta>, <param>, <script>, <style>, <title> Each Id should be unique in the page as rendered in the browser, which may or may not be all in the same file Can be used as anchor reference in URL Is referenced in CSS or URL with # sign Is referenced in JavaScript with getElementById(), and jQuery by $(#<id>) Shares same name space as name attribute Must contain at least one character Must begin with a letter Must not contain anything other than letters, numbers, underscores (_), dashes (-), colons (:), or periods (.) Is case insensitive
在(X)HTML5中,一切都是一样的,除了:
名称属性
在<form>上无效 XHTML规定必须全部小写,但大多数浏览器不遵守这一规定
Id属性
对任何元素都有效 XHTML规定必须全部小写,但大多数浏览器不遵守这一规定
这个问题是在HTML4.01是标准的时候写的,许多浏览器和功能都与今天不同。
添加一些对W3C文档的实际引用,以权威地解释'name'属性在表单元素上的作用。(不管怎样,我是在探索Stripe.js如何与支付网关Stripe实现安全交互时来到这里的。具体来说,是什么导致表单输入元素被提交回服务器,或者阻止它被提交?)
以下W3C文档是相关的:
Section 17.2控件
HTML 5: https://www.w3.org/TR/html5/forms.html#form-submission-0和 https://www.w3.org/TR/html5/forms.html#constructing-the-form-data-set节4.10.22.4构造表单数据集。
如文中所述,当且仅当输入元素具有有效的'name'属性时,浏览器才会提交输入元素。
正如其他人所注意到的,'id'属性唯一地标识DOM元素,但不涉及正常的表单提交。(虽然JavaScript当然可以使用'id'或其他属性来获取表单值,然后JavaScript可以将其用于Ajax提交等等。)
关于前面的回答/评论,一个奇怪的问题是id的值和name的值在同一个名称空间中。据我所知,从规范中可以看出,这适用于name属性的一些已弃用的用法(不是在表单元素上)。例如https://www.w3.org/TR/html5/obsolete.html:
"Authors should not specify the name attribute on a elements. If the attribute is present, its value must not be the empty string and must neither be equal to the value of any of the IDs in the element's home subtree other than the element's own ID, if any, nor be equal to the value of any of the other name attributes on a elements in the element's home subtree. If this attribute is present and the element has an ID, then the attribute's value must be equal to the element's ID. In earlier versions of the language, this attribute was intended as a way to specify possible targets for fragment identifiers in URLs. The id attribute should be used instead."
显然,在这个特殊情况下,'a'标记的id值和name值之间有一些重叠。但这似乎是片段id处理的一种特性,而不是由于id和名称的命名空间普遍共享。
推荐文章
- 使伸缩项目正确浮动
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?
- 如何删除和清除所有的本地存储数据
- 强制打开“另存为…”弹出打开文本链接点击PDF在HTML
- 如何修改标签文本?
- 在HTML中还有其他有用的空格码吗,比如半空格的 , em-spaces, en-spaces等等?
- 输入触发器按钮单击