在函数中创建模态表单对话框

时间:2012-06-01 04:32:27

标签: jquery

我的应用程序适用于许多模态窗体对话框,我想通过调用函数创建模态窗体对话框,但是当我在对话框“i.apply不是函数”上单击“确定”按钮时出现错误。我的代码在下面

html:

<div id="dlg_srch_acnt">
        <table cellpadding="0" cellspacing="0" border="0" class="display" id="dtl_acnt_srch" style=" padding-bottom:0px;">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Account name</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>

脚本

function init_dlg(id, autoOpen, height, width, modal, fn_button1)
            {
                id.dialog({
                    autoOpen:autoOpen,
                    height:height,
                    width:width,
                    modal:modal,
                    buttons:{
                        'OK':fn_button1,
                    },
                    close:fn_close
                });
            }



function fn_ok()
            {
                $('#parnt_acnt').val(acnt_name);
                $('#dlg_srch_acnt').('close');
            }

init_dlg($('#dlg_srch_acnt'), false, '440', '480', true, 'fn_ok()');

4 个答案:

答案 0 :(得分:3)

我相信您的$('#dlg_srch_acnt').('close');应为$('#dlg_srch_acnt').dialog('close');

并在参数中传递函数fn_ok而不是fn_ok(),因为它是它的执行值。

init_dlg($('#dlg_srch_acnt'), false, '440', '480', true, 'fn_ok');

acnt_name函数中的fn_ok是什么?定义它(在使用之前)或传递字符串,而不是变量名。

答案 1 :(得分:0)

当您将jquery对象传递给函数时,

$('#dlg_srch_acnt')。所以你需要像这样用jquery包装你的id

function init_dlg(id, autoOpen, height, width, modal, fn_button1)
            {
                $(id).dialog({ // Jquery wrapping should be done . 
                    autoOpen:autoOpen,
                    height:height,
                    width:width,
                    modal:modal,
                    buttons:{
                        'OK':fn_button1,
                    },
                    close:fn_close
                });
            }

答案 2 :(得分:0)

如果你只有一个按钮

,可能会删除fn_button1之后的逗号
buttons:{
          'OK':fn_button1
        },

答案 3 :(得分:0)

这里有几个问题。

Working jsFiddle Demo

0.1。正如Rajat指出的那样,$('#dlg_srch_acnt').('close');应为$('#dlg_srch_acnt').dialog('close');

0.2。关于传递函数名称,Rajat 几乎正确。应该是这样的(没有引号): init_dlg($('#dlg_srch_acnt'), false, '440', '480', true, fn_ok);

0.3。从jQUI对话框对象内部引用外部函数时,仍必须将它们包装在功能块中:

    buttons: {
        'OK': function(){
            fn_button1();
        }
    },
    close: function(){
        fn_close();
    }

*请注意,您没有在原始帖子*

中定义fn_close

这是完整的代码,添加了一些内容以显示它的工作情况(特别是,在dlg关闭时注意BOB更改为JANE):

<强> HTML:

<input id="parnt_acnt" type="text" value="bob" />
<div id="dlg_srch_acnt">
    <p>Inside the dialog</p>
</div>

<强>的javascript / jQuery的:

var acnt_name = "jane";

function init_dlg (id, autoOpen, height, width, modal, fn_button1 ) {
    id.dialog({
        autoOpen: autoOpen,
        height: height,
        width: width,
        modal: modal,
        buttons: {
            'OK': function(){
                fn_button1();
            }
        },
        close: function(){
            fn_close();
        }
    });
}

init_dlg( $('#dlg_srch_acnt'), false, '440', '480', true, fn_ok );

function fn_ok() {
    $('#parnt_acnt').val(acnt_name);
    $('#dlg_srch_acnt').dialog('close');
}

function fn_close() {
    $('#dlg_srch_acnt').dialog('close');
    alert('Close fn accessed');
}

Working jsFiddle Demo

相关问题