简体   繁体   English

函数未定义-jQuery / Javascript

[英]Function not defined - jQuery/Javascript

Another hapless noob... Okay, so I've looked everywhere to try and find out how to make this jQuery/JavaScript function, I hope it's just a mistake in my syntax, but here's my code: 另一个不幸的菜鸟...好吧,所以我到处寻找并尝试如何制作此jQuery / JavaScript函数,我希望这只是我的语法中的一个错误,但这是我的代码:

<script type="text/javascript">

$(function()
{
        function loadNext(choice) {
            $("#thequestion").load("question.php", { ans: choice, id: "<?php echo $row['id']; ?>", nextId: "<?php echo $nextQuestionId; ?>" } );

            $("#graph").load("graph.php", { id:"<?php echo $row['id']; ?>", nextId: "<?php echo $nextQuestionId; ?>" } );
            alert("loadNext activated");
        }

});
</script>

I then call the loadNext(choice) function using an onclick event like so: onclick="loadNext(YorN);" 然后,我使用诸如此类的onclick事件调用loadNext(choice)函数: onclick="loadNext(YorN);" .

Following this I get a 'loadNext is not defined' error. 之后,我收到“未定义loadNext”错误。

Amongst the answers that I would accept are: "YU NO LEARN!" 我会接受的答案包括:“你不懂!” and "You are silly." 和“你很傻。”

Here is a similar problem. 这是一个类似的问题。

Thanks. 谢谢。

The loadNext function you just defined is local to the scope of your anonymous function. 您刚刚定义的loadNext函数位于匿名函数范围的本地。 If you want to access it globally, then define it globally (outside of your onready handler). 如果要全局访问它,则全局定义它(在onready处理程序之外)。

The outer "$(function().." you have on your first line limits your scope - the loadNext function is only available within this block and will be undefined outside of it. 您在第一行中使用的外部“ $(function()..””限制了您的范围-loadNext函数仅在此块内可用,并且在该块外未定义。

The easy way to get around this is to just define the "loadnext" function outside the $(function()..) block but this is wrong for two reasons: 解决此问题的简单方法是仅在$(function()..)块之外定义“ loadnext”函数,但这是错误的,原因有两个:

  1. This makes the function global across your entire application, and polluting the global namespace is a no-no. 这使得函数在整个应用程序中都是全局的,并且污染全局名称空间是不行的。
  2. Inline javascript like "onclick" is bad in general - this blurs the line between presentation and logic and makes debugging a pain. 内联javascript(例如“ onclick”)通常是不好的-这模糊了表示和逻辑之间的界线,并使调试变得很痛苦。

To get around this, use the jquery bind events to attach your event handlers; 为了解决这个问题,请使用jquery绑定事件来附加事件处理程序; ie,

$("#myfancybutton", click(function(){ loadNext(choice) }); within your $(function()..) bloc $(“#myfancybutton”,click(function(){loadNext(choice)});在您的$(function()..)内部

because you have a function inside a function 因为你在一个函数里面有一个函数

 (function()
{
        function loadNext(choice) {
            this is bad
        }

function loadNext(choice) {
            this is good
        }

onclick="loadNext()"

no probs 没有问题

为什么不将loadNext函数放在$(function(){})之外呢?

Because loadNext is not in global scope. 因为loadNext不在全局范围内。 You can define it in global scope, or even better solution is to use .click() to bind your click event. 您可以在全局范围内定义它,甚至更好的解决方案是使用.click()绑定您的click事件。

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

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