简体   繁体   English

<input>通过CSS支持多行

[英]<input> multi-line capable via CSS

Is there a way to get an <input /> -field in HTML to wrap lines if the text is longer than the field using CSS? 如果文本比使用CSS的字段长,有没有办法在HTML中使用<input /> -field来换行? I don't want to use <textarea /> as I want to avoid users entering hard line-breaks by pressing enter. 我不想使用<textarea />因为我想通过按Enter来避免用户输入强硬换行符。

No, sorry. 不,对不起 <input type=text> is single line by definition. 根据定义, <input type=text>是单行。 See the W3C document Forms in HTML Documents : 请参阅HTML文档中的W3C文档表单

text
    Creates a single-line text input control.

Using Dojo's Dijit TextArea form control, based off TextArea, you can have an input field which begins as a single line and expands as the user adds to it. 使用Dojo的Dijit TextArea表单控件,基于TextArea,您可以有一个输入字段,该字段以单行开头,并在用户添加时扩展。

See its documentation . 查看其文档

单独使用CSS无法实现所需,但您可以使用JavaScript来防止用户在<textarea>字段中输入换行符。

Look at this, http://www.echoecho.com/htmlforms08.htm 看看这个, http://www.echoecho.com/htmlforms08.htm

The wrap options are the most tricky part of text areas. 换行选项是文本区域中最棘手的部分。 If you turn wrap off the text is handled as one long sequence of text without linebreaks. 如果您将换行翻转,则文本将作为一个长序列文本处理,而不会出现换行符。 If you set it to virtual the text appears on your page as if it recognized linebreaks - but when the form is submitted the linebreaks are turned off. 如果您将其设置为虚拟文本,则文本会显示在页面上,就像它识别出换行符一样 - 但是在提交表单时,会关闭换行符。 If you set it to physical the text is submitted exactly as it appears on the screen - linebreaks included. 如果将其设置为物理,则文本的提交方式与屏幕上显示的完全相同 - 包括换行符。

Your best bet is use a textarea (with autogrow capabilities if you like), and then strip out the new lines when the form is submitted. 您最好的选择是使用textarea(如果您愿意,可以使用自动增长功能),然后在提交表单时删除新行。 Using php it would be something like this: 使用PHP它将是这样的:

$text = str_replace(array("\n","\r"),'',$_POST['text_field']);

This would have the desired effect of blocking newline characters. 这将具有阻止换行符所需的效果。 As others have pointed out it's not really possible to get multi-line input in an input field. 正如其他人指出的那样,在输入字段中输入多行输入是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM