我在一个表单中输入了这个文本:

<input type="text"
       cols="40" 
       rows="5" 
       style="width:200px; height:50px;" 
       name="Text1" 
       id="Text1" 
       value="" />

我试图让它接受多行输入。宽度和高度使框更大,但用户可以输入所有(s)他想要的文本,但它只填充一行。

我如何使输入更像一个文本区域?


你不能。在编写本文时,唯一被设计为多行的HTML表单元素是<textarea>。


检查:

http://www.w3.org/TR/html401/interact/forms.html#h-17.7

TEXTAREA元素创建了一个 多行文本输入控件


您需要使用文本区域来获得多行处理。

<textarea name="Text1" cols="40" rows="5"></textarea>


使用textarea

<textarea name="textarea" style="width:250px;height:150px;"></textarea>

在开始和结束标签之间不要留下任何空格,否则会留下一些空行或空格。


通过赋予word-break: break-word,可以使文本输入多行;属性。(仅在Chrome中测试)


你应该使用textarea来支持多行输入。

<textarea rows="4" cols="50">
Here you can write some text to display in the textarea as the default text
</textarea>

使用<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
    }
});

如果你需要多行文本区域与自动高度增加功能,你可以使用以下简单的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) "" > < / >文本区域


如果你正在使用React,你可以使用第三方UI库MUI。它有一个自定义的私有<Input>元素,带有多行选项。

正如LSE注释的那样,最终它被呈现为<textarea>。

  <FormControl>
    <InputLabel htmlFor="textContract">{`textContract`}</InputLabel>
    <Input
      id="textContract"
      multiline
      rows="30"
      type="text"
      value={props.textContract}
      onChange={() => {}}
    />
  </FormControl>

https://material-ui.com/components/text-fields/#multiline


如果您不需要编辑它,您可以使用

< span >文本在这里< / span >

它将随着文本展开。