简体   繁体   English

IE中的JavaScript运行时错误

[英]JavaScript runtime error in IE

I get this error with some JavaScript on my page: 我在页面上使用一些JavaScript时遇到此错误:

JavaScript runtime error: The value of the property 'ToggleValidators' is null or undefined, not a Function object

I have been trying to fix this for ages and I can't seem to get it right. 多年来,我一直在尝试修复此问题,但似乎无法正确解决。

What the script is supposed to do is turn off the required field validators on a page and then toggle them on when a button is pressed. 该脚本应该执行的操作是关闭页面上必需的字段验证器,然后在按下按钮时将其打开。

The JavaScript code: JavaScript代码:

<script type="text/javascript" src="Scripts/jquery-1.4.1.js">

$(document).ready(function () {
    $('body').fadeIn(500);

    //Turn off all validation = it's switched on dynamically
    $.each(Page_Validators, function (index, validator) {
        ValidatorEnable(validator, false);
    });
});

function ToggleValidators('g2') 
{
  $.each(Page_Validators, function (index, validator) {
        if (validator.validationGroup == g2) 
        {
            ValidatorEnable(validator, true);
        }
        else 
        {
            ValidatorEnable(validator, false);
        }
    });
}

And on the button: 并在按钮上:

<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" ValidationGroup="g2" OnClientClick="ToggleValidators('g2');" />

What's the problem with my code? 我的代码有什么问题?

The error message basically means that the function ToggleValidators cannot be found, ie it can be that it was never created. 该错误消息基本上意味着找不到函数ToggleValidators ,即可能是从未创建过。

It looks like your code is inside a script tag with src attribute. 看起来您的代码在具有src属性的script标签中。 You have to put your code in its own script element: 您必须将代码放入其自己的脚本元素中:

<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script>
    // your code here
</script>

Otherwise the code won't even be evaluated (and the function won't be created, just like the error message said). 否则,代码甚至都不会被评估(并且不会创建函数,就像错误消息中所说的一样)。

Of course there might be other problems with your code (such as the quotes around the parameter definition). 当然,您的代码可能还会存在其他问题(例如,参数定义周围的引号)。

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

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