Javascript不能在IE和Chrome上工作(它可以在Firefox上运行)
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上运行)
先生/女士您的回答会有很大的帮助。谢谢++
我建议通过验证程序(如http://www.jslint.com/)运行您的代码,以确保所有内容都符合通用标准。
你指的是什么“通用标准”? ECMA-262? W3C DOM? ISO8601? JSLint用于ECMAScript,它不会修复任何与主机对象或其方法和属性有关的问题。 – RobG 2012-08-06 03:18:39
其他浏览器使用e.keyCode
来告诉你哪个键被按下。跨浏览器:
var k = e.keyCode || e.which;
还要确保您使用k
,而不是每次都重复e.which
。
我的印象是,jQuery将它标准化为''在'keypress'上的'which'。从[docs](http://api.jquery.com/keypress/):“当浏览器使用不同的属性来存储这些信息时,jQuery规范化.which属性,以便您可以可靠地使用它来检索字符代码。” – vcsjones 2012-08-06 00:48:46
所有代码都不是必需的。如果你想测试一个输入的值是唯一的数字,然后像下面会做什么:
<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);
}
它更友好的给用户一个关于您所需要的格式提示和等待,直到他们要么离开在提供有关无效值的警告之前控制或提交表单。你真的不关心用户如何获得有效值,只要表单提交时有效。
而且您必须再次在服务器上进行验证。
当你说“不工作”时,你的意思是什么:根本不工作或不按预期工作(然后你必须解释什么)。 – Nivas 2012-08-06 00:45:50
http://jsfiddle.net/ABCPY/采用缩进格式。 a)这个脚本的目的是什么?b)你是否知道你的整个ord函数只返回str.charCodeAt(0)或null? – Doug 2012-08-06 00:59:19
你似乎没有调用'ord()'。为什么代码是相关的? – jfriend00 2012-08-06 01:48:34