問題描述
id
和 name
屬性有什么區別?它們似乎都具有提供標識符的相同目的.
What is the difference between the id
and name
attributes? They both seem to serve the same purpose of providing an identifier.
我想知道(特別是關于 HTML 表單)是否有必要或出于任何原因鼓勵使用兩者.
I would like to know (specifically with regards to HTML forms) whether or not using both is necessary or encouraged for any reasons.
推薦答案
name
屬性在表單提交中發送數據時使用.不同的控件響應不同.例如,您可能有幾個具有不同 id
屬性但 name
相同的單選按鈕.提交后,響應中只有一個值 - 您選擇的單選按鈕.
The name
attribute is used when sending data in a form submission. Different controls respond differently. For example, you may have several radio buttons with different id
attributes, but the same name
. When submitted, there is just the one value in the response - the radio button you selected.
當然,它的意義遠不止這些,但它肯定會讓你朝著正確的方向思考.
Of course, there's more to it than that, but it will definitely get you thinking in the right direction.
這篇關于HTML中id和name屬性的區別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!