键入时文本框大小应该增长 [英] Text box size should grow when typing

查看:95
本文介绍了键入时文本框大小应该增长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本框中寻找效果。
最初文本框的大小很小,当输入时,文本框应该变大。是否有任何jquery插件可用于此效果?如果不能如何实现。?

I am looking for a effect in the text box. Initially the size of the text box will be small and when type in, the text box should grow bigger. Is there any jquery plugin available for this effect? If not how can this be achieved.?

推荐答案

$("#mytextbox").keyup(function() {
    $(this).attr("maxLength", $(this).attr("maxLength") + 1);
    $(this).attr("size", $(this).attr("size") + 1);
});

请注意,如果用户按下功能键且文本框聚焦,则无论大小如何都会增加。您应该监视文本框中的值的变化:

Be aware that if the user presses a function key with the textbox focused, the size will increase regardless. You should instead monitor the textbox for changes in its value:

$("#mytextbox").keyup(function() {
    $(this).attr("size", $(this).val().length + 1);
});

请注意,第二种方法也支持复制/粘贴到输入字段,而第一种方法将支持不是,虽然您必须确保用户有足够的空间来复制内容(现在,大小设置为当前值的+1,这意味着用户在大小之前一次只能输入一个字符增加)。

Note that the second method will also support copy/pasting into the input field, whereas the first one will not, although you'll have to make sure the user has enough room to copy something in (right now, the size is set to +1 of the current value, meaning the user can only input one character at a time before the size is increased).

这篇关于键入时文本框大小应该增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆