简体   繁体   English

JQUERY / JAVASCRIPT无法在移动设备上运行

[英]JQUERY / JAVASCRIPT won't work in mobile devices

I got a message system which requires a minimum amount of characters. 我有一个消息系统,它要求最少的字符数。 Therefore I use aa submit button which is disabled by default. 因此,我使用一个默认情况下禁用的提交按钮。 When the character limit is passed the submit button needs to be enabled. 超过字符数限制后,需要启用提交按钮。

The current code I use is: 我使用的当前代码是:

  $(window).load(function(){
     var amountArray = new Array(85,90,95,120);
     $('#message').keyup(function(){
         var length = $(this).val().length;
         if(length < amountArray[0]){
            $('.verzendenMSG').css('color','red');
         }else if(length < amountArray[1]){
            $('.verzendenMSG').css('color','darkorange');
         }else if(length < amountArray[2]){
            $('.verzendenMSG').css('color','olive');
            $('.verzendenMSG').removeAttr('disabled');
          }else if(length >= amountArray[3]){
            $('.verzendenMSG').css('color','green');
            $('.verzendenMSG').removeAttr('disabled');
         }
     });

With this code you get on desktop browsers (also Safari) a red text in the submit button and when you get closer to the limit the color is more green and when you get to the defined minimum the disable option of the submit button will disappear. 使用此代码,您可以在桌面浏览器(也包括Safari)上在“提交”按钮中看到红色文本,并且当接近极限时,颜色变为绿色,并且当达到定义的最小值时,“提交”按钮的禁用选项将消失。

This all won't work in a mobile browser, such as the ipad browser and the windows phone 7 browser. 所有这些都无法在移动浏览器(例如ipad浏览器和Windows Phone 7浏览器)中运行。

My question is, what could be the issue and how can I solve the issue. 我的问题是,可能是什么问题,我该如何解决。 Since this code is actually really simple? 既然这段代码实际上真的很简单?

Try replacing $(window) with $(document) like this: 尝试像这样用$(document)替换$(window)

  $(document).ready(function(){
     var amountArray = new Array(85,90,95,120);
     $('#message').keyup(function(){
         var length = $(this).val().length;
         if(length < amountArray[0]){
            $('.verzendenMSG').css('color','red');
         }else if(length < amountArray[1]){
            $('.verzendenMSG').css('color','darkorange');
         }else if(length < amountArray[2]){
            $('.verzendenMSG').css('color','olive');
            $('.verzendenMSG').removeAttr('disabled');
          }else if(length >= amountArray[3]){
            $('.verzendenMSG').css('color','green');
            $('.verzendenMSG').removeAttr('disabled');
         }
     });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM