Jquery在函数之间传递参数 [英] Jquery passing a parameter between functions

查看:115
本文介绍了Jquery在函数之间传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想从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链接 http://jsfiddle.net/bbKw8/

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.

为什么你应该使用'var'

这篇关于Jquery在函数之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆