是否有文字输入maxlength在Safari中不起作用的解决方法? [英] Is there a workaround for text input maxlength not working in Safari?

查看:311
本文介绍了是否有文字输入maxlength在Safari中不起作用的解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OS X 10.10.1的safari版本8.0(10600.1.25.1)中,如果您有以下内容:

 < input type =textmaxlength =5> 

用5个字符填充它,然后将插入符号(用鼠标或键盘)放在中间的字符串(不是在开头或结尾),打字会导致更多的字符被输入。



具体来说,它的作用就像忽略在计算字符串的长度时插入字符,因此,如果插入符号最初放置在位置1,则会有9个字符可以进入。

这不能在最新的Chrome或Firefox。



效果可以在这个页面看到: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_maxlength



我是使用maxlength错误?这是一个已知的解决方法吗?我试了一下,没有喜悦......

解决方案

试试这个:
HTML:

 < input name =mytesttype =text> 

Jquery:

 <如果(this.value.length> = 5){
返回false; $($'$'$)'('input [name =mytest b $ b}
});


In safari version 8.0 (10600.1.25.1) on OS X 10.10.1 if you have the following:

<input type="text" maxlength="5" >

Fill it with 5 characters, and then position the caret (with the mouse or keyboard) somewhere in the middle of the string (not at the very beginning or end), typing will cause more characters to be entered.

Specifically, it acts like it is ignoring the characters after the caret when calculating the "length" of the string, so if the caret was originally placed at position 1, 9 more characters will be enterable.

This is not reproducible on the latest chrome or firefoxes.

The effect can be seen on this page: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_maxlength

Am I using maxlength wrong? Is this a known thing with workarounds? I've googled a bit with no joy...

解决方案

Try this: HTML :

<input name="mytest" type="text">

Jquery :

$('input[name="mytest"]').keypress(function() {
    if (this.value.length >= 5) {
        return false;
    }
});

这篇关于是否有文字输入maxlength在Safari中不起作用的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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