Dijit对话框按钮事件未执行

时间:2014-03-22 20:48:22

标签: javascript jquery dojo

我以编程方式创建了 Dijit Dialog (jsfiddle)。调用Dialog Show时,我会在运行时传递按钮的id。我正在尝试创建单击事件函数来执行在运行时传递的id。

但是事件没有执行。我想知道如何将功能附加到对话框按钮。

HTML

<button id="show">Show Dialog</button> 

JS

require([
    "dijit/form/CheckBox",
    "dijit/dijit",
    "dijit/form/Textarea",
    "dijit/form/FilteringSelect",
    "dijit/form/TextBox",
    "dijit/form/ValidationTextBox",
    "dijit/form/DateTextBox",
    "dijit/form/TimeTextBox",
    "dijit/form/Button",
    "dijit/form/RadioButton",
    "dijit/form/Form",
    "dijit/_DialogMixin"]);
require([
    "dojo/parser",
    "dojo/_base/declare",
    "dojo/domReady!",
    "dojo/ready"],

function (parser, declare, ready, Dialog) {
    parser.parse();

    dojo.ready(function () {
        function showDialog(yesBtnId, noBtnId) {
            var dialog = new dijit.Dialog({
                title: 'Confirmation',
                content: '<table style= "width: 300px;">' +
                    '<tr>' +
                    '<th style="text-align:center; padding: 5px" colspan="2"><label>Are You Sure ?</label></th>' +
                    '</tr>' +
                    '<tr>' +
                    '<td style="text-align:center; padding: 5px"><button id=' + yesBtnId + '>Yes</button></td>' +
                    '<td style="text-align:center;padding: 5px"><button id= ' + noBtnId + '>No</button></td>' +
                    '</tr>' +
                    '</table>'
            });

            dialog.show();
        }

        var myShowBtn = {
            id: "show",
            onClick: function (evt) {

                showDialog('yesDelete', 'noDelete');
            }
        };
        dojo.query("#show").connect("onclick", myShowBtn.onClick);

        var myYesDelete = {
            id: "yesDelete",
            onClick: function (evt) {

                alert('hello its working....');

            }
        };

        dojo.query("#yesDelete").connect("onclick", myYesDelete.onClick);

    });
});

1 个答案:

答案 0 :(得分:2)

您正在尝试为调用它时尚不存在的元素添加事件处理程序,因此事件处理程序实际上没有绑定,因为此时没有任何东西可以将它绑定到。

但是,如果你移动这一行:

dojo.query("#yesDelete").connect("onclick", myYesDelete.onClick);

最多showDialog()功能(将其放在dialog.show()后面),然后它会在您更新的JSFiddle中看到。