开发者

Text field without form

I have a web page and I want to do something with user input text. Normally, <input type="text" /> is used inside a <form> nesting and setting input without form attribute is not acceptable in html strict schema. I want to avoid <form>, but still want some text field to use with JavaScript.

I am seeking an example with an option for text input and alert input data on 开发者_JS百科click of a button, with/without jQuery.


Using W3C's validator, this passes HTML 4.01 Strict:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test</title>
</head>
<body>
<p><input type="text" value="hi"></p>
</body>
</html>

and this passes XHTML 1.0 Strict:

<!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>
<p><input type="text" value="hi" /></p>
</body>
</html>

The problem is that <input> is an inline tag and thus needs to be inside something that accepts such tags (<body> does not).


Having a closer look at the DTD, an input element is part of the inline (lets call it) group:

<!ENTITY % formctrl "INPUT | SELECT | TEXTAREA | LABEL | BUTTON">

<!-- %inline; covers inline or "text-level" elements -->
<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">

So wherever inline is allowed, so is input. That means you don't need a form.

E.g. in a div:

<!ENTITY % flow "%block; | %inline;">
(...)
<!ELEMENT DIV - - (%flow;)*            -- generic language/style container -->

But not as you mentioned in a comment, in the body:

<!ELEMENT BODY O O (%block;|SCRIPT)+ +(INS|DEL) -- document body -->

(I think it goes without saying that block does not contain inline).


It's perfectly OK to have an input element without a form. Where does it say it's not allowed?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜