【发布时间】:2015-02-09 12:48:00
【问题描述】:
我在我的 html 页面中使用输入类型文本。现在我想根据输入文本字段的宽度来限制字符。我的输入字段宽度为 400 像素,我想将字符限制为字段宽度的 95%。
使用 maxlength 属性限制最大字符不适合我的工作流程。
谁能给点建议。
【问题讨论】:
我在我的 html 页面中使用输入类型文本。现在我想根据输入文本字段的宽度来限制字符。我的输入字段宽度为 400 像素,我想将字符限制为字段宽度的 95%。
使用 maxlength 属性限制最大字符不适合我的工作流程。
谁能给点建议。
【问题讨论】:
$(function(){
var noc= .95*parseInt($("#txt").width())/parseInt($("#txt").css("font-size"));
$("#txt").attr("maxlength",parseInt(noc));
$("#maxlengthfound").text(parseInt(noc));
})
#txt{width:200px;font-size:10px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id='txt'/>
<label id='maxlengthfound'></label>
【讨论】: