简体   繁体   English

IE8中的对象预期的JScript错误

[英]Object Expected JScript Error in IE8

Mostly dis can be possibly duplicate. 大多数情况下,dis可能是重复的。 But I dont know why I get this error. 但是我不知道为什么我得到这个错误。 My Script code . 我的脚本代码。

    $(document).ready(function () {        
            $(function () {
                $('input[name=Quantity]').blur(allownumbers);
            });

           function allownumbers() {
            alert('ad');
            var elements = document.getElementsByName('Quantity');
            alert(elements);
            for (var i = 0; i < elements.length; i++) {  
            if (elements[i].value == '' || elements[i].value < 1) {
                alert('Please enter a valid value');                    
                return false;
            }
            else if (elements[i].value > 100) {
                alert('Please enter a value less than 100');                
                return false;
            }
            }               
            return true;            
            }​
    });

My page code : 我的页面代码:

   <input id="Quantity" type="text" class="TxtBox" name="Quantity" value="@item.Quantity" onblur="return allownumbers()" maxlength="3"/> 

And the script references which I have was 我所拥有的脚本参考是

<script src="~/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>  
<script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript">

What's wrong with my code. 我的代码有什么问题。 Any suggestions .. EDIT : 任何建议..编辑:

The function is not even called when I dont call it in the textbox. 当我不在文本框中调用该函数时,甚至不会调用该函数。

You have duplicated your script references. 您已经复制了脚本引用。 You need only include each script once (preferrably the minified version unless it is a script you are actively debugging and writing). 您只需包含每个脚本一次(最好是压缩版本,除非它是您正在积极调试和编写的脚本)。 Secondly your initialization routine needs some help. 其次,您的初始化例程需要一些帮助。

$(document).ready(function () {        
        $('input[name=Quantity]').blur(allownumbers);
});

function allownumbers() {
        alert('ad');
        var elements = document.getElementsByName('Quantity');
        alert(elements);
        for (var i = 0; i < elements.length; i++) {  
        if (elements[i].value == '' || elements[i].value < 1) {
            alert('Please enter a valid value');                    
            return false;
        }
        else if (elements[i].value > 100) {
            alert('Please enter a value less than 100');                
            return false;
        }
        }               
        return true;            
        }​

Now then, the allownumbers does not have to be outside of the original block, but for me it helps keep my scope clean. 现在,allownumbers不必在原始块之外,但是对我来说,这有助于保持作用域清洁。 Secondly, $(document).ready ensures that your document is ready for use, so $(function () {}) is not necessary for JQuery call. 其次,$(document).ready确保您的文档已准备就绪,可以使用$(function(){})来进行JQuery调用。

So eliminate duplicate script references, and you should be good. 因此,消除重复的脚本引用,您应该会很好。

Your allownumbers function doesn't exist at global scope (it's nicely contained within your outermost ready handler, which is good; the global scope is polluted enough already), but that's where the old-style DOM0 handler hooked up via the onblur="..." attribute on your Quantity field's markup is looking for it. 您的allownumbers函数在全局范围内不存在(很好地包含在最外层的ready处理程序中,这很好;全局范围已经受到足够的污染),但这是通过onblur="..."连接旧式DOM0处理程序的地方onblur="..."属性添加到您的“ Quantity字段的标记中。 That's why when the field blurs, you get that error. 这就是为什么当场模糊时会出现该错误。

Since you're already hooking it up correctly via jQuery later (in your inner ready handler), just remove the onblur attribute entirely from the field markup. 由于您已经稍后通过jQuery(在内部的 ready处理程序中)正确地将其连接了,因此只需从字段标记中完全删除onblur属性即可。

Other notes: 其他说明:

  • You're using ready twice: First explicitly, then again inside that callback implicitly by passing a function reference into $ , which is a shorthand form of the same thing. 您要使用两次ready :首先是显式的,然后是在回调中隐式地通过将函数引用传递给$ ,这是同一事物的简写形式。 You can remove the inner one, it's not doing anything other than calling the function inside it. 您可以删除内部函数,除了调用内部函数外,它没有做任何其他事情。

  • You're loading both the unminified and minified scripts. 您正在加载未缩小和缩小的脚本。 Pick one set, not both. 选择一套,而不是两者都选。

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

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