[英]Function with arguements inside jQuery
Can someone please help why this is not working. 有人可以帮忙为什么这不起作用。
HTML Code: HTML代码:
<li><a href="#" onclick="adminDBDisplay(ownorg);" >OOrg</a></li>
<li><a href="#" onclick="adminDBDisplay(twoorg);" >Twoorg</a></li>
jQuery: jQuery的:
$(document).ready(function(){
function adminDBDisplay(db){
alert("Got into function" +db);
// Planning to use jQuery Ajax here
}
});
When I look using Firebug, I get following error: 使用Firebug时,出现以下错误:
"ReferenceError: adminDBDisplay is not defined"
Can someone please help me why this is not working. 有人可以帮我为什么这不起作用。 There something wrong on how I am approaching this.
我如何解决这个问题。 Please let me know if there are better ways.
如果有更好的方法,请告诉我。 Thanks for your help.
谢谢你的帮助。
When defining a function with function name() {...}
, if you are already inside a function, then it will only be defined in that function. 当使用
function name() {...}
定义函数时,如果您已经在函数内部,则只能在该函数中进行定义。
Function definitions should not be wrapped in a .ready()
, since they don't run until they are called. 函数定义不应包装在
.ready()
,因为它们在被调用之前不会运行。 Remove the .ready()
wrapper and you should be good to go. 删除
.ready()
包装器,您应该一切顺利。
The following notation: 以下表示法:
$(function() { ... });
itself is a function that binds function() {...}
to document.ready
. 本身是一个将
function() {...}
绑定到document.ready
function() {...}
。 And running function adminDBDisplay(db){...}
in document.ready
only defines the function it at runtime, but doesn't actually call it. 在
document.ready
运行函数adminDBDisplay(db){...}
仅在运行时定义该函数,而实际上并未调用它。 In order to call it, you would want adminDBDisplay('parameter');
为了调用它,您需要
adminDBDisplay('parameter');
To effectively implement it, you'll want to place it within the body of your page as follows: 为了有效地实现它,您需要按如下所示将其放置在页面的正文中:
<head>
<script src="yourversionofjquery.js"></script>
<script>
//Defining your function for calling later
function adminDBDisplay(db)
{
alert("Got into function" +db);
//Planning to use Jquery Ajax here
}
</script>
</head>
<body>
<script>
//Binding an anonymous function to document.ready
$(function () {
//Do whatever you want after document.ready
});
</script>
<div> rest of body</div>
</body>
Let me know if that makes sense/fixes your errors. 让我知道这是否有意义/修复了您的错误。
EDIT: also, put 'single quote' around 'ownorg', like "adminDBDisplay('ownorg');" 编辑:同样,在“ ownorg”周围加上“单引号”,例如“ adminDBDisplay('ownorg');” otherwise it's looking for a variable called ownorg.
否则,它正在寻找一个名为ownorg的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.