如何在扩展功能中将参数传递给jquery UI对话框按钮功能?

时间:2012-02-15 09:52:27

标签: jquery-ui jquery jquery-plugins

我想用jquery UI对话框创建弹出窗口,参数recordtype将决定要加载的对话框的内容以及按下对话框按钮时要调用的函数。

function showPopup(div_id, w, h, title, recordtype, recordid) {

    $(div_id).dialog({
        height: h,
        width: w,
        title: title,
        modal: true,
        autoOpen: true

    });

    //----extend the ui feature-----
    $.extend($.ui.dialog.prototype, {
        'addbutton': function(buttonName, func) {
            var buttons = this.element.dialog('option', 'buttons');
            buttons[buttonName] = func;
            this.element.dialog('option', 'buttons', buttons);
        }
    });

    //----load content page------
    if (recordtype == 'research') {
        $(div_id).load('ResearchContents.aspx?ID=' + recordid);
        $(div_id).dialog('addbutton', 'Save', onclick);
        $(div_id).dialog('addbutton', 'Delete', onclick);
    }
    else if (recordtype == 'course') {
        $(div_id).load('CourseContents.aspx?ID=' + recordid);
        $(div_id).dialog('addbutton', 'Create', onclick);
        $(div_id).dialog('addbutton', 'Delete', onclick);
    }

}

//-----How to pass parameters to onClick? ----------
function onclick(recordtype, recordid) {
   if(recordtype=='research' && recordid==-1)
       createResearch();
   else if(recordtype=='research' && recordid>-1)
       updateResearch();
   else if(recordtype=='course' && recordid==-1)
       createCourse();
   else if(recordtype=='course' && recordid>-1)
       updateCourse();

}

我的问题是,如何将参数recordtype和recordid传递给onClick?

1 个答案:

答案 0 :(得分:3)

如果你只是将refence作为回调传递给函数,那么在执行事件处理程序时,你唯一的参数就是ony jquery,这是一个Event对象。

将对“onClick”的调用包装到一个匿名函数中,这样你就可以调用方法传递你想要的参数:

$(div_id)
    .dialog('addbutton', 'Save', function() {
        onclick(recordtype, recordid);
    });
相关问题