简体   繁体   English

JavaScript无法在IE和Chrome上运行?(在Firefox上运行)

[英]Javascript not working on IE and Chrome?(it works on 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();
        }
    });
});

The code works on firefox but not on IE and chrome? 该代码适用于Firefox,但不适用于IE和chrome吗?

Sir/Ma'am your answers would be of great help. 先生/女士,您的回答会很有帮助。 Thank you++ 谢谢++

我建议通过诸如http://www.jslint.com/之类的验证器运行您的代码,以确保所有内容都符合通用标准。

Other browsers use e.keyCode to tell you which key was pressed. 其他浏览器使用e.keyCode告诉您按下了哪个键。 Cross-browser: 跨浏览器:

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

Also make sure you use k rather than repeating e.which every time. 还要确保使用k而不是每次都重复e.which

All that code is not required. 不需要所有这些代码。 If you want to test that an input's value is only digits, then something like the following will do: 如果要测试输入值仅是数字,则将执行以下操作:

<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);
}

It's much more friendly to give the user a hint about the format you require and wait until they either leave the control or submit the form before offering a warning about invalid values. 向用户提示您所需的格式,并等到他们离开控件或提交表单之后再提供有关无效值的警告,这将更加友好。 You really don't care how the user gets to a valid value, just so long as it's valid when the form is submitted. 您真的不在乎用户如何获得有效值,只要在提交表单时该值有效即可。

And you must validate on the server again. 并且您必须再次在服务器上进行验证。

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

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