问题描述
嘿,我想从jquery中的函数传递一个参数,但我不能让它工作。我究竟做错了什么?我想将点击功能中的名称传递给对话框功能以防止重复代码
Hey i would to pass a parameter from a function in jquery, but i cant get it to work. what am I doing wrong? I would like to pass "name" from the click function to the dialog block function to prevent repeating of code
更新:我已定义名称功能之后。然而,我仍然不能像这样传递<%= GetUserName(name)%>
Update: I have define name after the function. however i still cannot pass it like this <%=GetUserName(name)%>
更新2 jsFiddle链接
Update 2 jsFiddle link http://jsfiddle.net/bbKw8/
<div id="dialog-block" >
<p>
<b>Friends List:</b>
<% =GetFriends(0) %>
</p>
</div>
<div id="Div1">
<p>
<b>Friends List:</b>
<% =GetFriends(0) %>
</p>
</div>
<script type="text/javascript">
// the jQuery document ready handler
$(function () {
var name;
// create our dialog
$('#dialog-block').dialog({
title: '<%=GetUserName(name)%>',
autoOpen: false,
width: 400,
buttons: {
"Close": function () {
closeDialog($(this))
}
}
});
// the button to open the dialog
$('#user0chair,#apDiv1').click(function (event) {
if (this.id == 'user0chair') {
$('#dialog-block').dialog('open');
name = 1;
}
else if (this.id == 'user1chair') {
$('#dialog-block').dialog('open');
}
});
});
function closeDialog(elem) {
elem.dialog("close");
}
</script>
推荐答案
因为您未能定义使用
,它已被页面上的任何其他代码访问。 var
关键字命名
Because you failed to define name
with the var
keyword, it is already accessible by any other code on the page.
除了这种特殊情况,变量具有函数范围,因此在高于需要访问它的两个函数的级别定义 var name ...
。
Other than this special case, variables have function scope, so define a var name...
at the level higher than both functions that require access to it.
这篇关于Jquery在函数之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!