带有jquery和javascript函数的onkeydown

带有jquery和javascript函数的onkeydown,javascript,Javascript,我正在开发一个ASP.NET MVC 5 Web应用程序,我有以下html: <div class="group"> <input type="text" class="productClass" name="Configurations[0].RemainingCodes" id="Configurations[0].RemainingCodes" onkeydown='I

我正在开发一个ASP.NET MVC 5 Web应用程序,我有以下html:

<div class="group">
    <input type="text"
            class="productClass"
            name="Configurations[0].RemainingCodes"
            id="Configurations[0].RemainingCodes"
            onkeydown='IsValidKey(event);'
            required />
</div>
但是FireFox抱怨窗口事件不存在

我需要能够在尽可能多的浏览器上运行此代码

这不是重复的,因为我在Firefox中获得了代码,并且该函数允许输入字母


如何解决此问题?

您可以在Firefox中使用以下代码:

key = event.which;   

我在jQuery中总是这样做,而且在浏览器支持方面没有问题

$(document).keydown(function (e) {
        var c = e.which;
        e.preventDefault;
        return (c >= 48 && c <= 57) || c == 8 || c == 46 || (c >= 96 && c <= 105);
    });
$(文档).keydown(函数(e){
var c=e,其中:;
e、 防止违约;
return(c>=48&&c=96&&c
IsValidKey(this)
没有传入
event
对象,而是传入html元素。要传入事件,必须指定
event
类似的内容:
IsValidKey(this,event)
。您还必须在内联js中使用
return
,否则需要在回调中调用
evt.preventDefault()

函数IsValidKey(元素,evt){
var事件=((window.event)?(window.event):(evt));

return(event.keyCode>=48&&event.keyCode=96&&event.keyCode根据某人发布但他/她删除的答案,这是我的解决方案:

function IsValidKey(e) {
    var c = e.which;

    if (!((c >= 48 && c <= 57) || c == 8 || c == 46 || (c >= 96 && c <= 105)))
        e.preventDefault();
}
函数IsValidKey(e){
var c=e,其中:;

如果(!((c>=48&&c=96&&c您需要返回onkeydown属性中的布尔值: onkeydown='return IsValidKey(事件);'

如果事件处理程序返回false,则会阻止事件的传播或冒泡


也可以看到这个答案:

onkeydown='return IsValidKey(event);'的可能重复项。
谢谢,但该函数中有一些东西允许我键入字母。
$(document).keydown(function (e) {
        var c = e.which;
        e.preventDefault;
        return (c >= 48 && c <= 57) || c == 8 || c == 46 || (c >= 96 && c <= 105);
    });
function IsValidKey(e) {
    var c = e.which;

    if (!((c >= 48 && c <= 57) || c == 8 || c == 46 || (c >= 96 && c <= 105)))
        e.preventDefault();
}
<div class="group">
    <input type="text"
            class="productClass"
            name="Configurations[0].PkgRatio"
            id="Configurations[0].PkgRatio"
            onkeydown='IsValidKey(event);'
            required />
</div>