Why won't this XHTML form validate?

二次信任 提交于 2019-12-11 01:05:44

问题


Any ideas why this won't validate here:

http://validator.w3.org/#validate_by_input

It seems the form input tags are wrong but reading through the XHTML spec they should validate fine. Any ideas?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>Test</title>
 </head>

 <body>
    <div class="Header">
        <table class="HeaderTable">
            <tr>
                <td>
                    <div class="Heading">Test <span class="Standard">Test</span>
                    </div>
                </td>
                <td>
                    <div class="Controls">
                        <form id="ControlForm" method="get" action="Edit.php">
                            <input type="submit" name="action" id="Edit" value="Edit" />
                            <input type="submit" name="action" id="New" value="New" />
                        </form>
                    </div>
                </td>
            </tr>
        </table>
    </div>
 </body>
</html>

回答1:


You need to move

<div class="Controls">

so that it's inside the <form tag


This validates nicely

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test</title>
</head>

<body>
    <div class="Header">
        <table class="HeaderTable">
        <tr>
            <td>
                <div class="Heading">Test <span class="Standard">Test</span></div>
            </td>
            <td>
                <form id="ControlForm" method="get" action="Edit.php">
                    <div class="Controls">
                        <input type="submit" name="action" id="Edit" value="Edit" />
                        <input type="submit" name="action" id="New" value="New" />
                    </div>
                </form>
            </td>
        </tr>
        </table>
    </div>
</body>
</html>



回答2:


Try putting a fieldset tag around the inputs. I think the idea of forms in XHTML is that they can't have direct descendants that aren't div, fieldset, etc.




回答3:


As someone else put it:

[quote] The validator is telling you that your hidden input element cannot immediately follow the form tag - it needs to have a container element of some kind. [/quote]

(Source)

I guess a fieldset could help; See The XHTML DTD:

<!ELEMENT form %form.content;>

<!ENTITY % form.content "(%block; | %misc;)*">

<!ENTITY % misc "noscript | %misc.inline;">
<!ENTITY % misc.inline "ins | del | script">

<!ENTITY % block "p | %heading; | div | %lists; | %blocktext; | fieldset | table">

<!ENTITY % heading "h1|h2|h3|h4|h5|h6">
<!ENTITY % lists "ul | ol | dl">
<!ENTITY % blocktext "pre | hr | blockquote | address">

No input for you :(




回答4:


I had the very same problem and it took me some time to figure out. Is this a recent change with the w3c validator? it's just I'm sure some of my pages with forms validated in the past, but now they all seem to through up errors for the same problem.

I used to always do something like:

<div>
<form>
    <label></label>
    <input />
    <label></label>
    <input />
    <label></label>
    <input />
</form>

and get validation errors, so now I just add fieldset or div around all the labels and inputs to get it to validate, like this:

<div>
<form>
    <fieldset>or<div>
        <label></label>
        <input />
        <label></label>
        <input />
        <label></label>
        <input />
    </fieldset>or</div>
</form>

Seems to work, but I'm sure I didn't have to do this in the past... hmmm?




回答5:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>Test</title>
 </head>

 <body>
    <div class="Header">
        <table class="HeaderTable">
                <tr>
                        <td>
                                <div class="Heading">Test <span class="Standard">Test</span>
                                </div>
                        </td>
                        <td>

                            <form id="ControlForm" method="get" action="Edit.php">
                                <div class="Controls">
                                                <input type="submit" name="action" id="Edit" value="Edit" />
                                                <input type="submit" name="action" id="New" value="New" />
                                </div>
                            </form>

                        </td>
                </tr>
        </table>
    </div>
 </body>
</html>

Put your div inside your form.




回答6:


Your input elements should be within a fieldset. This validates and has the added benefit of making the document more accessible to non-visual user agents.

As an aside, your markup is suffering from divitis a bit. You could put classes on the table cells directly rather than nesting div elements within them (I'm not going to comment on the use of tables for layout). Also, you could style the form element directly rather than nesting it within a div.

Anyway, here's your example with a fieldset added so it validates:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Test</title>
  </head>
  <body>
    <div class="Header">
      <table class="HeaderTable">
        <tr>
          <td>
            <div class="Heading">Test <span class="Standard">Test</span></div>
          </td>
          <td>
            <div class="Controls">
              <form id="ControlForm" method="get" action="Edit.php">
                <fieldset>
                  <input type="submit" name="action" id="Edit" value="Edit" />
                  <input type="submit" name="action" id="New" value="New" />
                </fieldset>
              </form>
            </div>
          </td>
        </tr>
      </table>
    </div>
  </body>
</html>


来源:https://stackoverflow.com/questions/253312/why-wont-this-xhtml-form-validate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!