HTML 5 difference input id and input name? [duplic

2020-05-20 04:06发布

问题:

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?

回答1:

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.



回答2:

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:

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.