admin管理员组文章数量:1326344
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:
<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);"
.
Following this I get a 'loadNext is not defined' error.
Amongst the answers that I would accept are: "Y U NO LEARN!" and "You are silly."
Here is a similar problem.
Thanks.
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:
<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);"
.
Following this I get a 'loadNext is not defined' error.
Amongst the answers that I would accept are: "Y U NO LEARN!" and "You are silly."
Here is a similar problem.
Thanks.
Share Improve this question edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Jan 18, 2012 at 1:24 user1104147user1104147 1053 silver badges9 bronze badges 05 Answers
Reset to default 4The loadNext
function you just defined is local to the scope of your anonymous function. If you want to access it globally, then define it globally (outside of your onready handler).
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.
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:
- This makes the function global across your entire application, and polluting the global namespace is a no-no.
- Inline javascript like "onclick" is bad in general - this blurs the line between presentation and logic and makes debugging a pain.
To get around this, use the jquery bind events to attach your event handlers; i.e.,
$("#myfancybutton", click(function(){ loadNext(choice) }); within your $(function()..) bloc
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
why dont you just put your loadNext function outside of the $(function(){}) ?
Because loadNext
is not in global scope. You can define it in global scope, or even better solution is to use .click() to bind your click event.
本文标签: Function not definedjQueryJavascriptStack Overflow
版权声明:本文标题:Function not defined - jQueryJavascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742199103a2431626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论