Javascript 错误:预期功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11946386/
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 14:50:29  来源:igfitidea点击:

Javascript error: function expected

javascript

提问by Faith Camille

I have this javascript and I get the error "function expected". I can't see anything wrong with my javascript. Please help. Thanks.

我有这个 javascript,我收到错误“预期功能”。我看不出我的 javascript 有什么问题。请帮忙。谢谢。

function checkrewardname()
{
var my=document.getElementById("Rname");
var con=my.value;
 var mine=document.getElementById("forref").value.split('\n');

if (con == "")
        {
            alert("Enter a Reward Name.");
    }
 else
    { 
    var i=0;
    while(i<=mine.length)
         {
            if (mine(i) == con)//error here
                {
                    alert("Duplicate reward. Please enter a new reward.");
                }
            else
                {
                    document.getElementById("validate").click();
                    alert("The reward has been saved.");
                }
            i++;
        }   
     }
}`

回答by Sean Vieira

mineis an array but you are calling it as if it were a function. Use mine[i]rather than mine(i)and you'll access the array by index rather than generating an error. (Just a note; most C-style languages use [and ]for array access and reserve (and )for function invocation).

mine是一个数组,但您将其称为函数。使用mine[i]而不是mine(i),您将通过索引访问数组而不是产生错误。(请注意;大多数 C 风格的语言使用[]用于数组访问和保留(以及)用于函数调用)。

回答by Faith Camille

You also have while(i<=mine.length)

你还有 while(i<=mine.length)

shouldn't it be while(i < mine.length)

不应该是 while(i < mine.length)