为什么 PHP 只使用“名称”标识符而不是“id”来解析 DOM 元素


Why does PHP only parse DOM elements using 'name' identifiers, and not 'id'?

必须

为表单中元素的ID和名称复制相同的字符串似乎有很多浪费:

<input id="foo" />            <!-- JS/CSS accept this -->
<input name="foo" />          <!-- Only PHP accepts this -->
<input id="foo" name="foo" /> <!-- Everyone's happy -->

为什么 PHP 不使用 id 标签?

为什么 PHP 不使用 id 标签?

那不是PHP,那是HTML。 PHP 与 HTML 规范无关。

HTML 确实使用id属性,但它将它们用于与name属性不同的目的。

HTML form元素从它们的子承载价值元素(inputselecttextarea等(构建请求(通常POSTGET(。 name属性用作键,value(或所选值等(用作值。

这将为请求中的数据创建键/值对。

必须为表单中元素的 id 和名称复制相同的字符串似乎有很多浪费

您不必复制它。 您可以亲自选择复制它。 但这是你的选择。 任何规范/标准/惯例/等中都没有规则表明您必须甚至应该这样做。

<小时 />
<input id="foo" />            <!-- JS/CSS accept this -->
<!--- incorrect.  JS/CSS can also target name attributes if necessary. -->
<input name="foo" />          <!-- Only PHP accepts this -->
<!--- incorrect.  This isn't PHP, this is HTML.  PHP isn't involved here at all. -->
<input id="foo" name="foo" /> <!-- Everyone's happy -->
<!--- if that's the markup you choose to define, sure. -->

为什么 PHP 不使用 id 标签?

因为这是将表单提交到指定服务器端的标准集。我会说ID最有可能与js/css一起使用,而你需要name来序列化你的表单元素。

Id 属性用于 DOM 操作。http://www.w3schools.com/tags/att_global_id.asp

发送表单时name使用属性。http://www.w3schools.com/tags/att_input_name.asp

这些是不同的目的。您也可以在 JS 中使用name属性,但将返回元素数组:

// plain JS
document.getElementsByName("foo");
// jQuery example
$('[name="foo"]')

因为在 web 中,每个唯一用途需要 3 个类型标识符,例如

1(类:用于设计和选择DOM中的多个标签(jQuery(
2( id :用于访问 DOM 中的某些特定元素,也用于设计一个字段
3(名称:用于传递单个和多个数据和数组

如果可以通过id传递值,那么如何发送复选框值和多个选择标签值???