我在一个表单中输入了这个文本:
<input type="text"
cols="40"
rows="5"
style="width:200px; height:50px;"
name="Text1"
id="Text1"
value="" />
我试图让它接受多行输入。宽度和高度使框更大,但用户可以输入所有(s)他想要的文本,但它只填充一行。
我如何使输入更像一个文本区域?
我在一个表单中输入了这个文本:
<input type="text"
cols="40"
rows="5"
style="width:200px; height:50px;"
name="Text1"
id="Text1"
value="" />
我试图让它接受多行输入。宽度和高度使框更大,但用户可以输入所有(s)他想要的文本,但它只填充一行。
我如何使输入更像一个文本区域?
当前回答
如果你需要多行文本区域与自动高度增加功能,你可以使用以下简单的javascript,
函数auto_height(elem) {/* javascript */ element .style.height = "1px"; element .style.height = (element . scrollheight)+"px"; } .auto_height {/* CSS */ 宽度:100%; } <textarea rows="1" class="auto_height" oninput="auto_height(this) "" > < / >文本区域
其他回答
你不能。在编写本文时,唯一被设计为多行的HTML表单元素是<textarea>。
如果您不需要编辑它,您可以使用
< span >文本在这里< / span >
它将随着文本展开。
如果你需要多行文本区域与自动高度增加功能,你可以使用以下简单的javascript,
函数auto_height(elem) {/* javascript */ element .style.height = "1px"; element .style.height = (element . scrollheight)+"px"; } .auto_height {/* CSS */ 宽度:100%; } <textarea rows="1" class="auto_height" oninput="auto_height(this) "" > < / >文本区域
使用<div contenteditable="true">(支持良好)和stored to <input type="hidden">。
HTML:
<div id="multilineinput" contenteditable="true"></div>
<input type="hidden" id="detailsfield" name="detailsfield">
jQuery:
$("#multilineinput").on('keyup',function(e) {
$("#detailsfield").val($(this).text()); //store content to input[type=hidden]
});
//optional - one line but wrap it
$("#multilineinput").on('keypress',function(e) {
if(e.which == 13) { //on enter
e.preventDefault(); //disallow newlines
// here comes your code to submit
}
});
检查:
http://www.w3.org/TR/html401/interact/forms.html#h-17.7
TEXTAREA元素创建了一个 多行文本输入控件