在另一个html表单中包含html表单是否有效?

本文翻译自:Is it valid to have a html form inside another html form?

Is it valid html to have the following: 是否具有以下内容是有效的html:

<form action="a">
    <input.../>
    <form action="b">
        <input.../>
        <input.../>
        <input.../>
    </form>
    <input.../>
</form>

So when you submit "b" you only get the fields within the inner form. 因此,当您提交“ b”时,您只会在内部表单中获得字段。 When you submit "a" you get all fields minus those within "b". 提交“ a”时,您将获得减去“ b”中所有字段的所有字段。

If it isn't possible, what workarounds for this situation are available? 如果不可能,那么针对这种情况有哪些解决方法可用?


#1楼

参考:https://stackoom.com/question/2Kca/在另一个html表单中包含html表单是否有效


#2楼

In case someone find this post here is a great solution without the need of JS. 万一有人发现这篇文章,这是不需要JS的绝佳解决方案。 Use two submit buttons with different name attributes check in your server language which submit button was pressed cause only one of them will be sent to the server. 使用两个具有不同名称属性的提交按钮,以您的服务器语言检查被按下的提交按钮,因为只有其中一个被发送到服务器。

<form method="post" action="ServerFileToExecute.php">
    <input type="submit" name="save" value="Click here to save" />
    <input type="submit" name="delete" value="Click here to delete" />
</form>

The server side could look something like this if you use php: 如果使用php,服务器端可能看起来像这样:

<?php
    if(isset($_POST['save']))
        echo "Stored!";
    else if(isset($_POST['delete']))
        echo "Deleted!";
    else
        echo "Action is missing!";
?>

#3楼

如果您需要表单将数据提交/提交到1:M关系数据库,我建议在表A上创建一个“插入后”数据库触发器,该触发器将为表B插入必要的数据。


#4楼

HTML 4.x & HTML5 disallow nested forms, but HTML5 will allow a workaround with "form" attribute ("form owner"). HTML 4.x和HTML5不允许使用嵌套表单,但是HTML5允许使用“表单”属性(“表单所有者”)进行变通。

As for HTML 4.x you can: 至于HTML 4.x,您可以:

  1. Use an extra form(s) with only hidden fields & JavaScript to set its input's and submit the form. 使用仅包含隐藏字段和JavaScript的额外表单来设置其输入内容并提交表单。
  2. Use CSS to line up several HTML form to look like a single entity - but I think that's too hard. 使用CSS排列几种HTML表单,使其看起来像一个实体-但这太难了。

#5楼

No, it is not valid. 不,这是无效的。 But a "solution" can be creating a modal window outside of form "a" containing the form "b". 但是,“解决方案”可以在包含形式“ b”的形式“ a”之外创建模式窗口。

<div id="myModalFormB" class="modal">
    <form action="b">
        <input.../>
        <input.../>
        <input.../>
        <button type="submit">Save</button>
    </form>
</div>

<form action="a">
    <input.../>
    <a href="#myModalFormB">Open modal b </a>
    <input.../>
</form>

It can be easily done if you are using bootstrap or materialize css. 如果您使用引导程序或实例化CSS,则可以轻松完成。 I'm doing this to avoid using iframe. 我这样做是为了避免使用iframe。


#6楼

不可以,HTML规范指出任何FORM元素都不应包含另一个FORM元素。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 HTML 页面添加一个表单,可以按照以下步骤操作: 1. 在 HTML 文件添加一个 `<form>` 标签,该标签用于包含表单元素。 2. 在 `<form>` 标签内部添加表单元素,例如输入框、单选框、复选框、下拉列表等等。每个表单元素都需要一个唯一的 `name` 属性,以便在提交表单时能够识别出每个元素的值。 3. 添加一个提交按钮,用户点击该按钮后,表单的数据将被提交到服务器。可以使用 `<input>` 标签创建提交按钮,`type` 属性设置为 `submit`。 4. 可以使用 `<label>` 标签为表单元素添加标签,这样用户就可以清楚地知道每个元素代表什么。 以下是一个简单的表单示例: ```html <form action="submit.php" method="post"> <label for="name">姓名:</label> <input type="text" id="name" name="name"><br> <label for="email">邮箱:</label> <input type="email" id="email" name="email"><br> <label for="gender">性别:</label> <input type="radio" id="male" name="gender" value="male"> <label for="male">男</label> <input type="radio" id="female" name="gender" value="female"> <label for="female">女</label><br> <label for="color">喜欢的颜色:</label> <select id="color" name="color"> <option value="red">红色</option> <option value="green">绿色</option> <option value="blue">蓝色</option> </select><br> <input type="submit" value="提交"> </form> ``` 在这个表单,用户需要输入姓名和邮箱,选择性别和喜欢的颜色,然后点击提交按钮。当用户点击提交按钮时,表单的数据将被提交到 `submit.php` 文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值