Can you nest html forms?

前端 未结 20 3095
悲&欢浪女
悲&欢浪女 2020-11-21 04:56

Is it possible to nest html forms like this

so

相关标签:
20条回答
  • 2020-11-21 05:15

    Although the question is pretty old and I agree with the @everyone that nesting of form is not allowed in HTML

    But this something all might want to see this

    where you can hack(I'm calling it a hack since I'm sure this ain't legitimate) html to allow browser to have nested form

    <form id="form_one" action="http://apple.com">
      <div>
        <div>
            <form id="form_two" action="/">
                <!-- DUMMY FORM TO ALLOW BROWSER TO ACCEPT NESTED FORM -->
          </form>
        </div>
          <br/>
        <div>
          <form id="form_three" action="http://www.linuxtopia.org/">
              <input type='submit' value='LINUX TOPIA'/>
          </form>
        </div>
          <br/>
    
        <div>
          <form id="form_four" action="http://bing.com">
              <input type='submit' value='BING'/>
          </form>
        </div>
          <br/>  
        <input type='submit' value='Apple'/>
      </div>  
    </form>
    

    JS FIDDLE LINK

    http://jsfiddle.net/nzkEw/10/

    0 讨论(0)
  • 2020-11-21 05:15

    Today, I also got stuck in same issue, and resolve the issue I have added a user control and
    on this control I use this code

    <div class="divformTagEx">
    
    </div>
    
    <asp:Literal runat="server" ID="litFormTag" Visible="false">
    '<div> <form  style="margin-bottom: 3;" action="http://login.php" method="post" name="testformtag"></form> </div>'</asp:Literal>
    

    and on PreRenderComplete event of the page call this method

    private void InitializeJavaScript()
    {
            var script = new StringBuilder();
            script.Append("$(document).ready(function () {");
            script.Append("$('.divformTagEx').append( ");
            script.Append(litFormTag.Text);
            script.Append(" )");
            script.Append(" });");
            ScriptManager.RegisterStartupScript(this, GetType(), "nestedFormTagEx", script.ToString(), true);
        }
    

    I believe this will help.

    0 讨论(0)
  • 2020-11-21 05:16

    Another way to get around this problem, if you are using some server side scripting language that allows you to manipulate the posted data, is to declare your html form like this :

    <form>
    <input name="a_name"/>
    <input name="a_second_name"/>
    <input name="subform[another_name]"/>
    <input name="subform[another_second_name]"/>
    </form>
    

    If you print the posted data (I will use PHP here), you will get an array like this :

    //print_r($_POST) will output :
        array(
        'a_name' => 'a_name_value',
        'a_second_name' => 'a_second_name_value',
        'subform' => array(
          'another_name' => 'a_name_value',
          'another_second_name' => 'another_second_name_value',
          ),
        );
    

    Then you can just do something like :

    $my_sub_form_data = $_POST['subform'];
    unset($_POST['subform']);
    

    Your $_POST now has only your "main form" data, and your subform data is stored in another variable you can manipulate at will.

    Hope this helps!

    0 讨论(0)
  • 2020-11-21 05:18

    If you're using AngularJS, any <form> tags inside your ng-app are replaced at runtime with ngForm directives that are designed to be nested.

    In Angular forms can be nested. This means that the outer form is valid when all of the child forms are valid as well. However, browsers do not allow nesting of <form> elements, so Angular provides the ngForm directive which behaves identically to <form> but can be nested. This allows you to have nested forms, which is very useful when using Angular validation directives in forms that are dynamically generated using the ngRepeat directive. (source)

    0 讨论(0)
  • 2020-11-21 05:20

    A simple workaround is to use a iframe to hold the "nested" form. Visually the form is nested but on the code side its in a separate html file altogether.

    0 讨论(0)
  • While I don't present a solution to nested forms (it doesn't work reliably), I do present a workaround that works for me:

    Usage scenario: A superform allowing to change N items at once. It has a "Submit All" button at the bottom. Each item wants to have its own nested form with a "Submit Item # N" button. But can't...

    In this case, one can actually use a single form, and then have the name of the buttons be submit_1..submit_N and submitAll and handle it servers-side, by only looking at params ending in _1 if the name of the button was submit_1.

    <form>
        <div id="item1">
            <input type="text" name="foo_1" value="23">
            <input type="submit" name="submit_1" value="Submit Item #1">
        </div>
        <div id="item2">
            <input type="text" name="foo_2" value="33">
            <input type="submit" name="submit_2" value="Submit Item #2">
        </div>
        <input type="submit" name="submitAll" value="Submit All Items">
    </form>
    

    Ok, so not much of an invention, but it does the job.

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