jQuery - 基于值的宽度调整表单输入大小? [英] jQuery - resizing form input based on value's width?

查看:130
本文介绍了jQuery - 基于值的宽度调整表单输入大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面是一个例子:

http://jsfiddle.net/bzBdX/2/
<这里是jQuery代码:

  $('input ').each(function(){
var value = $(this).val();
var size = value.length;
//使用大小属性
//$(this).attr('size',size);

//播放css宽度
size = size * 2; // char的平均宽度
$(this).css('width',size * 3);

});

http: //jsfiddle.net/bzBdX/7/


Is it possible to get input's value width and resize the input dynamically so the value will fit?

Here's an example:

http://jsfiddle.net/bzBdX/2/

解决方案

Here is the jQuery code :

$('input').each(function(){
var value = $(this).val();
var size  = value.length;
// playing with the size attribute
//$(this).attr('size',size);

// playing css width
size = size*2; // average width of a char
$(this).css('width',size*3);

})​;

http://jsfiddle.net/bzBdX/7/

这篇关于jQuery - 基于值的宽度调整表单输入大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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