2012-08-06 37 views
0
function ord(string) { 
    var str = string + '', 
     code = str.charCodeAt(0); 
    if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters) 
     var hi = code; 
     if (str.length === 1) { 
      return code; // This is just a high surrogate with no following low surrogate, so we return its value; 
      // we could also throw an error as it is not a complete character, but someone may want to know } 
      var low = str.charCodeAt(1); 
      return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000; 
     } 
     if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate return code; // This is just a low surrogate with no preceding high surrogate, so we return its value; 
      // we could also throw an error as it is not a complete character, but someone may want to know 
     } 
     return code; 
    } 
} 

$(document).ready(function() { 
    var maxTxtNumber = 8; 
    var arrTxtNumber = new Array(); 
    var txtvalues = new Array(); 
    var arr = {}; 

    $('.numericonly').keypress(function (e) { 
     var t = $(this).val(); 
     var k = e.which; 
     delete arr[8]; 
     if ((e.which >= 49 && e.which <= 55) || e.which == 8) { 
      if (e.which == 8) { 
       var s = new String(t); 
       s = s.charCodeAt(0); 
       delete arr[s]; 
      } 
      if (arr[k]) { 
       e.preventDefault(); 
      } else { 
       arr[k] = e.which; 
      } 
     } else { 
      e.preventDefault(); 
     } 
    }); 
}); 

該代碼適用於Firefox,但不適用於IE和Chrome?Javascript不能在IE和Chrome上工作(它可以在Firefox上運行)

先生/女士您的回答會有很大的幫助。謝謝++

+1

當你說「不工作」時,你的意思是什麼:根本不工作或不按預期工作(然後你必須解釋什麼)。 – Nivas 2012-08-06 00:45:50

+0

http://jsfiddle.net/ABCPY/採用縮進格式。 a)這個腳本的目的是什麼?b)你是否知道你的整個ord函數只返回str.charCodeAt(0)或null? – Doug 2012-08-06 00:59:19

+0

你似乎沒有調用'ord()'。爲什麼代碼是相關的? – jfriend00 2012-08-06 01:48:34

回答

0

我建議通過驗證程序(如http://www.jslint.com/)運行您的代碼,以確保所有內容都符合通用標準。

+0

你指的是什麼「通用標準」? ECMA-262? W3C DOM? ISO8601? JSLint用於ECMAScript,它不會修復任何與主機對象或其方法和屬性有關的問題。 – RobG 2012-08-06 03:18:39

0

其他瀏覽器使用e.keyCode來告訴你哪個鍵被按下。跨瀏覽器:

var k = e.keyCode || e.which; 

還要確保您使用k,而不是每次都重複e.which

+0

我的印象是,jQuery將它標準化爲''在'keypress'上的'which'。從[docs](http://api.jquery.com/keypress/):「當瀏覽器使用不同的屬性來存儲這些信息時,jQuery規範化.which屬性,以便您可以可靠地使用它來檢索字符代碼。」 – vcsjones 2012-08-06 00:48:46

0

所有代碼都不是必需的。如果你想測試一個輸入的值是唯一的數字,然後像下面會做什麼:

<input type="text" onblur="check(this);" ...> 


function check(el) { 
    if (!isDigits(el.value)) { 
    alert('Hey!!\nThe element you just left should only contain digits'); 
    } 
} 

function isDigits(s) { 
    return /^\d*$/.test(s); 
} 

它更友好的給用戶一個關於您所需要的格式提示和等待,直到他們要麼離開在提供有關無效值的警告之前控制或提交表單。你真的不關心用戶如何獲得有效值,只要表單提交時有效。

而且您必須再次在服務器上進行驗證。

相關問題