dse3168 2016-02-02 12:59
浏览 25
已采纳

为什么PHP只使用'name'标识符解析DOM元素,而不是'id'?

There seems to be a lot of waste in having to duplicate the same string for both the id and name of an element within a form:

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

Why does PHP not use id tags?

  • 写回答

4条回答 默认 最新

  • dougou8639 2016-02-02 13:31
    关注

    Why does PHP not use id tags?

    That's not PHP, that's HTML. PHP has nothing to do with the HTML spec.

    HTML does use id attributes, but it uses them for a different purpose than name attributes.

    HTML form elements build requests (POST or GET generally) from their child value-carrying elements (input, select, textarea, etc.). The name attribute is used as the key, and the value (or selected value, etc.) is used as the value.

    This creates the key/value pairs for the data in the request.

    There seems to be a lot of waste in having to duplicate the same string for both the id and name of an element within a form

    You don't have to duplicate it. You may personally choose to duplicate it. But that's your choice. There's no rule in any specs/standards/conventions/etc. indicating that you must or even should do that.


    <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. -->
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口