【问题标题】:In samsung S5, the maxlength attribute is not working在三星 S5 中,maxlength 属性不起作用
【发布时间】:2015-10-29 07:05:03
【问题描述】:

html 5 maxlength 不适用于三星设备。如何使用 javascript 限制用户在文本框中输入超过最大字符数。

【问题讨论】:

  • 您使用的是哪种浏览器?原生浏览器还是 Chrome?你确定你的代码吗?我们可以有你的 html 代码吗?我想要确认,因为我在三星 S4 和三星 S6 上尝试过,并且一切正常。如果你是对的,在设备中看到这样的错误,我会感到非常惊讶。

标签: javascript html samsung-mobile


【解决方案1】:

如果长度有效,您可以检查 keyup 事件...

old_value = "";

document.querySelector(yourinput).addEventListener("keyup", function(e){
    if (this.value.length > 20){
        //set the old value
        this.value == old_value;
    }
    else{
        old_value = this.value;
    }
})

这是未经测试只是一个想法如何处理这个。

(我认为这不会起作用,但很高兴知道)

document.querySelector(yourinput).maxLength = "20";
//ie: document.querySelector(yourinput).setAttribute("maxLength", 20);

【讨论】:

  • 如果三星设备不理解 maxlength 属性,我认为您的第二种方法不会完成这项工作。但是第一种方法可以做到。您可以使用 jquery,使用此属性处理所有输入元素并添加您的侦听器。
  • 因此它只是第二个选项。 -> 不需要 JQUERY,您可以轻松选择多个元素。而且 op 没有标记 jquery,所以不。
  • 是的!不需要 jquery。
猜你喜欢
  • 2015-09-04
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2015-10-07
  • 2014-02-24
  • 1970-01-01
相关资源
最近更新 更多