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条)

报告相同问题?