问题
Is it valid html to have the following:
<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. When you submit \"a\" you get all fields minus those within \"b\".
If it isn\'t possible, what workarounds for this situation are available?
回答1:
A. It is not valid HTML nor XHTML
In the official W3C XHTML specification, Section B. "Element Prohibitions", states that:
"form must not contain other form elements."
http://www.w3.org/TR/xhtml1/#prohibitions
As for the older HTML 3.2 spec, the section on the FORMS element states that:
"Every form must be enclosed within a FORM element. There can be several forms in a single document, but the FORM element can't be nested."
B. The Workaround
There are workarounds using JavaScript without needing to nest form tags.
"How to create a nested form." (despite title this is not nested form tags, but a JavaScript workaround).
Answers to this StackOverflow question
Note: Although one can trick the W3C Validators to pass a page by manipulating the DOM via scripting, it's still not legal HTML. The problem with using such approaches is that the behavior of your code is now not guaranteed across browsers. (since it's not standard)
回答2:
In case someone find this post here is a great solution without the need of 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
if(isset($_POST['save']))
echo "Stored!";
else if(isset($_POST['delete']))
echo "Deleted!";
else
echo "Action is missing!";
?>
回答3:
HTML 4.x & HTML5 disallow nested forms, but HTML5 will allow a workaround with "form" attribute ("form owner").
As for HTML 4.x you can:
- Use an extra form(s) with only hidden fields & JavaScript to set its input's and submit the form.
- Use CSS to line up several HTML form to look like a single entity - but I think that's too hard.
回答4:
As others have said, it is not valid HTML.
It sounds like your are doing this to position the forms visually within each other. If that is the case, just do two separate forms and use CSS to position them.
回答5:
No, the HTML specification states that no FORM
element should contain another FORM
element.
回答6:
rather use a custom javascript-method inside the action attribute of the form!
eg
<html>
<head>
<script language="javascript" type="text/javascript">
var input1 = null;
var input2 = null;
function InitInputs() {
if (input1 == null) {
input1 = document.getElementById("input1");
}
if (input2 == null) {
input2 = document.getElementById("input2");
}
if (input1 == null) {
alert("input1 missing");
}
if (input2 == null) {
alert("input2 missing");
}
}
function myMethod1() {
InitInputs();
alert(input1.value + " " + input2.value);
}
function myMethod2() {
InitInputs();
alert(input1.value);
}
</script>
</head>
<body>
<form action="javascript:myMethod1();">
<input id="input1" type="text" />
<input id="input2" type="text" />
<input type="button" onclick="myMethod2()" value="myMethod2"/>
<input type="submit" value="myMethod1" />
</form>
</body>
</html>
回答7:
You can answer your own question very easily by inputting the HTML code into the W3 Validator. (It features a text input field, you won't even have to put your code on a server...)
(And no, it won't validate.)
回答8:
A possibility is to have an iframe inside the outer form. The iframe contains the inner form. Make sure to use the <base target="_parent" />
tag inside the head tag of the iframe to make the form behave as part of the main page.
回答9:
no, see w3c
回答10:
No, it is not valid. But a "solution" can be creating a modal window outside of form "a" containing the form "b".
<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. I'm doing this to avoid using iframe.
回答11:
If you need your form to submit/commit data to a 1:M relational database, I would recommend creating an "after insert" DB trigger on table A that will insert the necessary data for table B.
回答12:
No, it is not valid but you can trick your inner form position on the the HTML
with a help of CSS
and jQuery
usage. First create some container div inside your parent form and then in any other place on the page the div with your child (inner) form:
<form action="a">
<input.../>
<div class="row" id="other_form_container"></div>
<input.../>
</form>
....
<div class="row" id="other_form">
<form action="b">
<input.../>
<input.../>
<input.../>
</form>
</div>
Give your container div some stable heaght
<style>
#other_form_container {
height:90px;
position:relative;
}
</style>
Thank trick the "other_form" position relatively to container div.
<script>
$(document).ready(function() {
var pos = $("#other_form_container").position();
$("#other_form").css({
position: "absolute",
top: pos.top - 40,
left: pos.left + 7,
width: 500,
}).show();
});
</script>
P.S.: You'll have to play with numbers to make it look nice.
来源:https://stackoverflow.com/questions/555928/is-it-valid-to-have-a-html-form-inside-another-html-form