最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Text field without form - Stack Overflow

programmeradmin0浏览0评论

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 click of a button, with/without jQuery.

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 click of a button, with/without jQuery.

Share edited Nov 18, 2019 at 21:22 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jun 26, 2011 at 11:32 AlfredAlfred 21.4k63 gold badges174 silver badges257 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 6

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3/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/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3/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 ment, 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)

  1. 暂无评论