Rak*_*yal 115 html javascript textarea
如果我写的话,我想有一些功能
<textarea maxlength="50"></textarea>
<textarea maxlength="150"></textarea>
<textarea maxlength="250"></textarea>
Run Code Online (Sandbox Code Playgroud)
它会自动在textArea上施加maxlength.如果可能请不要在jQuery中提供解决方案.
注意:如果我这样做,可以这样做:
<textarea onkeypress="return imposeMaxLength(event, this, 110);" rows="4" cols="50">
function imposeMaxLength(Event, Object, MaxLen)
{
return (Object.value.length <= MaxLen)||(Event.keyCode == 8 ||Event.keyCode==46||(Event.keyCode>=35&&Event.keyCode<=40))
}
Run Code Online (Sandbox Code Playgroud)
复制在HTML textarea上模拟HTML输入"maxlength"属性的最佳方法是什么?
但问题是我每次声明textArea时都不想写onKeyPress和onKeyUp.
Jos*_*ola 111
window.onload = function() {
var txts = document.getElementsByTagName('TEXTAREA');
for(var i = 0, l = txts.length; i < l; i++) {
if(/^[0-9]+$/.test(txts[i].getAttribute("maxlength"))) {
var func = function() {
var len = parseInt(this.getAttribute("maxlength"), 10);
if(this.value.length > len) {
alert('Maximum length exceeded: ' + len);
this.value = this.value.substr(0, len);
return false;
}
}
txts[i].onkeyup = func;
txts[i].onblur = func;
}
};
}
Run Code Online (Sandbox Code Playgroud)
Eir*_*k W 80
我知道你想避免使用jQuery,但由于解决方案需要JavaScript,因此这个解决方案(使用jQuery 1.4)是最简洁和最强大的.
受到启发,但对Dana Woodman的回答有所改进:
这个答案的变化是:简化和更通用,使用jQuery.live,如果长度正常,也不设置val(导致IE中的工作箭头键,以及IE中的显着加速):
// Get all textareas that have a "maxlength" property. Now, and when later adding HTML using jQuery-scripting:
$('textarea[maxlength]').live('keyup blur', function() {
// Store the maxlength and value of the field.
var maxlength = $(this).attr('maxlength');
var val = $(this).val();
// Trim the field if it has content over the maxlength.
if (val.length > maxlength) {
$(this).val(val.slice(0, maxlength));
}
});
Run Code Online (Sandbox Code Playgroud)
编辑:jQuery 1.7+的更新版本,使用on而不是live
// Get all textareas that have a "maxlength" property. Now, and when later adding HTML using jQuery-scripting:
$('textarea[maxlength]').on('keyup blur', function() {
// Store the maxlength and value of the field.
var maxlength = $(this).attr('maxlength');
var val = $(this).val();
// Trim the field if it has content over the maxlength.
if (val.length > maxlength) {
$(this).val(val.slice(0, maxlength));
}
});
Run Code Online (Sandbox Code Playgroud)
Dan*_*man 33
更新使用Eirik的解决方案,.live()因为它更健壮.
即使你想要一个不使用jQuery的解决方案,我想我会为通过Google找到这个页面并寻找jQuery-esque解决方案的人添加一个:
$(function() {
// Get all textareas that have a "maxlength" property.
$('textarea[maxlength]').each(function() {
// Store the jQuery object to be more efficient...
var $textarea = $(this);
// Store the maxlength and value of the field.
var maxlength = $textarea.attr('maxlength');
var val = $textarea.val();
// Trim the field if it has content over the maxlength.
$textarea.val(val.slice(0, maxlength));
// Bind the trimming behavior to the "keyup" event.
$textarea.bind('keyup', function() {
$textarea.val($textarea.val().slice(0, maxlength));
});
});
});
Run Code Online (Sandbox Code Playgroud)
希望对你有用的Google员工......
jam*_*iss 31
HTML5 maxlength为textarea元素添加了一个属性,如下所示:
<!DOCTYPE html>
<html>
<body>
<form action="processForm.php" action="post">
<label for="story">Tell me your story:</label><br>
<textarea id="story" maxlength="100"></textarea>
<input type="submit" value="Submit">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Chrome 13,FF 5和Safari 5目前支持此功能.毫无疑问,IE 9不支持此功能.(在Win 7上测试过)
此解决方案避免了IE中的问题,其中当添加文本中间的字符时删除最后一个字符.它也适用于其他浏览器.
$("textarea[maxlength]").keydown( function(e) {
var key = e.which; // backspace = 8, delete = 46, arrows = 37,38,39,40
if ( ( key >= 37 && key <= 40 ) || key == 8 || key == 46 ) return;
return $(this).val().length < $(this).attr( "maxlength" );
});
Run Code Online (Sandbox Code Playgroud)
我的表单验证然后处理用户可能粘贴的任何问题(在IE中似乎只是一个问题)文本超过textarea的最大长度.
| 归档时间: |
|
| 查看次数: |
229402 次 |
| 最近记录: |