問題描述
id
和 name
屬性有什么區(qū)別?它們似乎都具有提供標(biāo)識(shí)符的相同目的.
What is the difference between the id
and name
attributes? They both seem to serve the same purpose of providing an identifier.
我想知道(特別是關(guān)于 HTML 表單)是否有必要或出于任何原因鼓勵(lì)使用兩者.
I would like to know (specifically with regards to HTML forms) whether or not using both is necessary or encouraged for any reasons.
推薦答案
name
屬性在表單提交中發(fā)送數(shù)據(jù)時(shí)使用.不同的控件響應(yīng)不同.例如,您可能有幾個(gè)具有不同 id
屬性但 name
相同的單選按鈕.提交后,響應(yīng)中只有一個(gè)值 - 您選擇的單選按鈕.
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.
當(dāng)然,它的意義遠(yuǎn)不止這些,但它肯定會(huì)讓你朝著正確的方向思考.
Of course, there's more to it than that, but it will definitely get you thinking in the right direction.
這篇關(guān)于HTML中id和name屬性的區(qū)別的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!