简体   繁体   English

脚本错误:“ c不是函数”

[英]script error: 'c is not a function"

I get the following javascript error when I debug in firebug: 在Firebug中调试时,出现以下javascript错误:

'cities' is not a function. 

What I'm trying to achieve is dynamic options and it used to work, this is just a new version of the page which has broken the old function that I need to restore. 我想要实现的是动态选项,它过去一直有效,这只是页面的新版本,它破坏了我需要恢复的旧功能。 It's called from HTML: 从HTML调用:

<select name="w" onchange="cities(this);" id="searcharea_expanded" class="">

And it is declared as javascript in the same file: 并在同一文件中声明为javascript:

<script>
function cities(obj){

    if(obj.value == '3'){

         //undisplay cities options municipality_control2

        document.getElementById('municipality_control').style.display='none'

    }else{

        $('#cities').load('/cities?regionId='+obj.value);
    }
}  
}
</script>

So why am I getting this error? 那我为什么会收到这个错误? I also have a div named cities which is the div that the script shall update and this used to work: 我也有一个名为city的div,它是脚本将更新的div,它可以正常工作:

<div id="cities" class="selectbox munics ">
     <select id="municipality_control" name="m">
         <option value="4691207">Madgaon</option>
         <option value="4695203">Mormugao</option>
         <option value="4692204">Panaji</option>
         <option value="4676203">Other city</option>
    </select>
</div>

The problem is that you are closing more braces than you've opened. 问题是您要关闭的括号比打开的要多。 If you use jQuery, use its functions. 如果您使用jQuery,请使用其功能。 For example instead of document.getElementById("id") use $("#id") . 例如,使用$("#id")代替document.getElementById("id") $("#id")

Your code will be: 您的代码将是:

<script>
    function cities(obj){
       if(obj.val() == '3'){
         // undisplay cities options municipality_control2
         // document.getElementById('municipality_control').style.display='none'
         $('#municipality_control').hide()
       } else {
         $('#cities').load('/cities?regionId='+obj.val());
       }
    }  
    // } << This has to be removed
</script>

I also recommend you to prevent using HTML attributes for Javascript like onchange="..." . 我还建议您避免使用Java的HTML属性,例如onchange="..." Use the following, instead: 请使用以下内容:

$("#searcharea_expanded").on("change", function () {
     cities($(this));
});

Well, assuming that's a direct copy+paste from your site, then the problem is easy: you have an extra closing brace } at the end of the script. 好吧,假设这是您网站上的直接复制+粘贴,那么问题就很容易了:脚本末尾有一个额外的右括号} This is a syntax error that will make the entire script block invalid, which means that the function will never be defined. 这是一个语法错误,它将使整个脚本块无效,这意味着该函数将永远无法定义。

if you indent your code properly, you'd notice this kind of thing more easily. 如果您正确缩进代码,您会更容易注意到这种情况。 In addition, I recommend using a decent editor or IDE to write your code, which does syntax highlighting. 另外,我建议使用体面的编辑器或IDE编写代码,从而突出显示语法。 For example, opening your code in Netbeans immediately puts an error marker on the relevant line, making it extremely easy to see the problem. 例如,在Netbeans中打开您的代码会立即在相关行上放置一个错误标记,从而非常容易发现问题。

There are at least three possibilities that I can see: 我至少可以看到三种可能性:

  • Your cities() function gets called (maybe because you change the select ) before the script that defines it is interpreted. 在解释定义该脚本的脚本之前,您的cities()函数将被调用(可能是因为changeselect )。 Verify that the <script> opens and closes before the HTML. 验证<script>是否在HTML之前打开和关闭。 Possibly, add the onchange listener via jQuery instead of inlining it. 可能通过jQuery添加onchange侦听器,而不是对其进行内联。

  • There's an error in the Javascript preventing the cities declaration from being executed at all. Javascript中有一个错误,根本无法执行cities声明。 Inspect the error console or Javascript console or whatever your browser supplies to intercept Javascript errors. 检查错误控制台或Javascript控制台,或检查您的浏览器提供的任何内容,以拦截Javascript错误。

  • Your cities objects gets overwritten somehow by another cities (possibly holding the jQuery handle to the cities div ?). 你的cities对象被另一个莫名其妙地覆盖cities (可能持有jQuery的句柄cities div ?)。

I ordered the possibilities in decreasing order of likelihood based on my own mistakes. 我下令减少基于自己错误可能性顺序的可能性。

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

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