3

当您提交表单时,表单是否会同时查找“name”和“id”值?“id”除了用 CSS 设置样式外还能做什么?

<select name="combobox" id="combobox">
   <option value="0">Inactive</option>
   <option value="1">Active</option>
</select>
4

5 回答 5

7

Name

The name attribute is needed for the database or other data destination to uniquely identify that piece of data.

When the form is submitted, most scripts use the name attribute to place the form data into a database or into an email that can be read by a person. Thus, if the <input> element is for the site visitor to enter their name into, then the name attribute would be name="name" or name="first-name", etc. (source (W3C)).

A control's "control name" is given by its name attribute. The scope of the name attribute for a control within a FORM element is the FORM element. Source (W3C).

Id

The id attribute has several roles in HTML:

  • As a style sheet selector.
  • As a target anchor for hypertext links.
  • As a means to reference a particular element from a script.
  • As the name of a declared OBJECT element.
  • For general purpose processing by user agents (e.g., for identifying fields when extracting data from HTML pages into a database, translating HTML documents into other formats, etc.) (Source (W3C).)
于 2013-04-28T04:03:57.320 回答
1

name表示将使用密钥发送值。

id通常使用,以便 JavaScript 可以找到元素。

为方便起见,它们可能相同,但不一定。

于 2013-04-28T04:02:02.610 回答
1

当您提交表单时,表单是否会同时查找“name”和“id”值?

不,只是名称属性

“id”除了用 CSS 设置样式外还能做什么?

您可以使用id来引用表单元素集合中的元素。

假设我们有一个由变量引用的表单元素form,您可以访问表单中的元素(表单元素、输入、选择等)。使用id,例如combobox,您可以通过form['combobox']或访问此元素form.comboboxname属性也是如此- http://jsfiddle.net/YQRtR/

于 2013-04-28T04:18:19.260 回答
0

name将出现在查询字符串中,即

?name=value

所以在你的情况下:

?combobox=0 OR ?combobox=1

取决于用户的选择。

id被 CSS 用来添加样式,类似于class除了一个特定的id只能分配给一个 DOM 元素。也可id用于使用 JavaScript 操作 DOM 上的元素,因为它允许识别元素。

于 2013-04-28T04:02:19.670 回答
0

You can refer to developer.mozilla.org or http://htmldog.com/ for details about HTML.

id

The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document).

The id attribute is most used to point to a style in a style sheet, and by JavaScript (via the HTML DOM) to manipulate the element with the specific id.

name

The name attribute specifies the name of a form.

The name attribute is used to reference elements in JavaScript code, or to reference form data after a form is submitted.

于 2013-04-28T04:09:13.927 回答