javascript 为什么“Microsoft JScript 运行时错误:预期对象”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9028867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:22:18  来源:igfitidea点击:

Why "Microsoft JScript runtime error: Object expected"?

javascriptjqueryasp.net-mvc-3

提问by ideAvi

I'm about 1 day old in using jquery, and is currently having a nightmare with it. I alreadt spent half of my day trying to get rid of this error. I did some reading after “googling” the error (sorry, Bing!) and discovered that most of these errors result from the jquery file not being properly loaded. Okay…that started to point me in the right direction but I still couldn't figure out why it wasn't pathing properly. I mean, I was doing as people said – I would drag the .js file into my designer and it would print out the proper path, but still the error shows.

我使用 jquery 大约 1 天,目前正在做噩梦。我已经花了半天时间试图摆脱这个错误。我在“谷歌搜索”错误后做了一些阅读(对不起,必应!)并发现这些错误中的大多数是由于未正确加载 jquery 文件造成的。好的……这开始为我指明正确的方向,但我仍然无法弄清楚为什么它没有正确路径。我的意思是,我按照人们说的去做——我将 .js 文件拖到我的设计器中,它会打印出正确的路径,但仍然显示错误。

Here's my exact code in my editor template (with the error):

这是我的编辑器模板中的确切代码(有错误):

@model bool
@{
    string status = "Active";
    string buttonValue = "Deactivate";
    string hiddenValue = "true";
    if (!ViewData.Model)
    {
       status = "Inactive";
       buttonValue = "Activate";
       hiddenValue = "false";
    }
}

<div style="width:100px; float:left;"> 
<img id = "AD_Img" src = "/Content/themes/base/images/icon_@(status).png" alt = @(status) />
<label for = "AD_Img" id = "AD_Label" >@(status)</label>
</div>
<div style="width:100px; float:left;"> 
<input type="button" id = "AD_Button" value = @(buttonValue) style = "width:100px" onclick = "ChangeStatus()" />
<input id = "AcntStatus" type = "hidden" name = "AcntStatus" value = @(hiddenValue) />
</div>

and in the same cshtml file, the script goes this way:

在同一个 cshtml 文件中,脚本是这样的:

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

function ChangeStatus()             
{
    var ButtonVal = $("#AD_Button").val();
    alert(ButtonVal);

        if (ButtonVal == "Deactivate")
        {
            var stat = "Inactive";
            var buttonVal = "Activate";
            var HiddenValue = "false";
        }

        else if (ButtonVal == "Activate")
        {
            stat = "Active";
            buttonVal = "Deactivate";
            HiddenValue = "true";
        }
            $("#AD_Img").attr({src: "/Content/themes/base/images/icon_"+stat+".png", alt: stat});
            $("#AD_Label").html(stat);
            $("#AD_Button").val(buttonVal);
            $("#AcntStatus").val(HiddenValue);    
}
 </script>

The debugger stops on the ChangeStatus function of the input element on the following line:

调试器在以下行的 input 元素的 ChangeStatus 函数上停止:

    <input type="button" id = "AD_Button" value = @(buttonValue) style = "width:100px" onclick = "ChangeStatus()" />

i tried to debug it by using this in my function code:

我试图通过在我的函数代码中使用它来调试它:

    function ChangeStatus()             
    {
       var ButtonVal = document.getElementById("AD_Button").value;
       alert(ButtonVal);
    }

And it works properly, it returns the exact string that I'm looking for without that error, but why? What's wrong with my codes? Please help me figure this out.

它工作正常,它返回我正在寻找的确切字符串,没有那个错误,但为什么呢?我的代码有什么问题?请帮我解决这个问题。

采纳答案by Dr.Molle

This:

这:

$("#AD_Img").attr(src: "../../../Content/themes/base/images/icon_"+stat+".png", alt: stat);

forces an Syntax-error. It has to be:

强制语法错误。它一定要是:

$("#AD_Img").attr({src: "../../../Content/themes/base/images/icon_"+stat+".png", alt: stat});


Edit:

编辑:

Also take a look at your <script>, you can't mix external JS and internal JS.

还要看看你的<script>,你不能混合外部JS和内部JS。

This:

这:

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

Has to be splitted into

必须分成

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

<script type="text/javascript">
//your code
</script>