HTML 5 difference input id and input name? [duplic

2020-05-20 03:19发布

I'm busy with something for school in HTML 5.

So here is my bit of code

<label for="name">Name</label>
<input type="text" id="name" name="name" placeholder="your name" required><br>

So my question actually is:

What is the difference between the NAME and the ID? purpose? which one is more important?

标签: html input
3条回答
Ridiculous、
2楼-- · 2020-05-20 03:51

The name attribute is for submitting a form element to the server; many elements may share the same name (e.g. radio buttons, which must have the same name within the set).

The id attribute is for uniquely identifying any element (not just form elements). It must be unique throughout the entire document.

查看更多
何必那么认真
3楼-- · 2020-05-20 03:52

The id attribute is supposed to be unique in your document. Only one element can have a given id. document.getElementById() finds the first element with the given id.

The name attribute is used by forms as the key in a key/value pair when submitting the form. The value attribute is both displayed in the browser, and submitted with the form.

Neither is "more important," they're just different. If you have an XML mindset, they're both just attributes on a node. In HTML they have more meaning though.

查看更多
forever°为你锁心
4楼-- · 2020-05-20 03:56

In short, the name is the identifier that is sent to the server when you submit the form. The id is a unique identifier for the browser, clientside, for javascript and such.

查看更多
登录 后发表回答