Are nested forms valid in HTML5?

后端 未结 2 1751
我在风中等你
我在风中等你 2020-12-18 19:17

Nested Forms Acceptable?

I can\'t find anything in the HTML5 doc that talks about nested forms. I\'m sure it\'s listed on some page, somewhere (perhaps a changelog

相关标签:
2条回答
  • 2020-12-18 19:50

    If they were supported then something like this would work:

    $('button').on('click', function(e) {
        var form = $(this).parents('form');
        e.preventDefault();
        if(!form) {
            form = $(this);
        }
        $('#output').val('from '+form.attr('id')+'\n'+form.serialize());
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <form id='parent'>
        <form id='childA'>
            <input type='text' placeholder='value of A'/>
        <button>Send part A</button>
    </form>
        <form id='childB'>
            <input type='text' placeholder='value of B'/>
        <button>Send part B</button>
    </form>
        <button>Send All</button>
    </form>
    <br/>
    <textarea id='output'></textarea>

    However because it is not officially supported you can look see the browser is preventing it (closing what it assumes are open-ended form elements). One alternative would be to have a single form but add data attributes to the individual inputs so JavaScript can collect the sub fields you want and manually build a POST from them.

    0 讨论(0)
  • 2020-12-18 19:53

    The HTML5 document does mention it in the section you link above:

    Content model

    Flow content, but with no form element descendants.

    "Content model" means "what this element may contain". So no, nested forms are not allowed.

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