Is it correct to use DIV inside FORM?

前端 未结 10 1606
旧巷少年郎
旧巷少年郎 2020-12-02 17:51

I\'m just wondering what are you thinking about DIV-tag inside FORM-tag?

I need something like this:

&l
相关标签:
10条回答
  • 2020-12-02 18:22

    It is wrong to have <input> as a direct child of a <form>

    And by the way <input /> may fail on some doctype

    Check it with http://validator.w3.org/check


    document type does not allow element "INPUT" here; missing one of "P", "H1", "H2", "H3", "H4", "H5", "H6", "PRE", "DIV", "ADDRESS" start-tag

    <input type="text" />

    The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.

    One possible cause for this message is that you have attempted to put a block-level element (such as "<p>" or "<table>") inside an inline element (such as "<a>", "<span>", or "<font>").

    0 讨论(0)
  • 2020-12-02 18:24

    As the others have said, it's all good, you can do it just fine. For me personally, I try to keep a form of hierarchical structure with my elements with a div being the outer most parent element. I try to use only table p ul and span inside forms. Just makes it easier for me to keep track of parent/children relationships inside my webpages.

    0 讨论(0)
  • 2020-12-02 18:32

    No, its not

    <div> tags are always abused to create a web layout. Its symbolic purpose is to divide a section/portion in the page so that separate style can be added or applied to it. [w3schools Doc] [W3C]

    It highly depends on what your some and another has.

    HTML5, has more logical meaning tags, instead of having plain layout tags. The section, header, nav, aside everything have their own semantic meaning to it. And are used against <div>

    0 讨论(0)
  • 2020-12-02 18:33

    You can use a <div> within a form - there is no problem there .... BUT if you are going to use the <div> as the label for the input dont ... label is a far better option :

    <label for="myInput">My Label</label> 
    <input type="textbox" name="MyInput" value="" />
    
    0 讨论(0)
  • 2020-12-02 18:33

    I noticed that whenever I would start the form tag inside a div the subsequent div siblings would not be part of the form when I inspect (chrome inspect) henceforth my form would never submit.

    <div>
    <form>
    <input name='1st input'/>
    </div>
    <div>
    <input name='2nd input'/>
    </div>
    <input type='submit'/>
    </form>
    

    I figured that if I put the form tag outside the DIVs it worked. The form tag should be placed at the start of the parent DIV. Like shown below.

    <form>
    <div>
    <input name='1st input'/>
    </div>
    <div>
    <input name='2nd input'/>
    </div>
    <input type='submit'/>
    </form>
    
    0 讨论(0)
  • 2020-12-02 18:35

    It is totally fine .

    The form will submit only its input type controls ( *also Textarea , Select , etc...).

    You have nothing to worry about a div within a form.

    0 讨论(0)
提交回复
热议问题