jquery - 动态定义选项

发布于 2024-10-06 11:54:14 字数 1111 浏览 2 评论 0原文

所以只是为了描述我正在尝试做的事情:我正在尝试创建一种简单的方法来创建模式消息。尝试减少代码重复(我不想为所有内容创建对话框)。因此,希望我能够定义模态的标题、内容和按钮,然后调用函数(doModal())。

由于某种原因,下面的代码没有正确传递按钮(没有显示按钮)。如果我简单地用这个替换modal.buttons

{
                thisone: function(){
                    alert('you clicked this one');
                }
            }

那么它就会起作用。但是我无法找到一种简单的方法来定义按钮,这违背了我想要做的事情。

这是当前的代码:

var modal = $('<div id="modal"><p></p></div>');
function doModal()
{
    var modal = $('<div id="modal"><p></p></div>');
    modal.text(modal.content);
    modal.attr('title', modal.title);
    modal.dialog('destroy');
    modal.dialog({
        modal:true,
        resizable:false,
        draggable:false,
        buttons: modal.buttons
    });
}

$(document).ready(function(){
    modal.title = 'Are you sure?';
    modal.content = 'Are you sure? Deleting a product cannot be undone.';
    modal.buttons = {
            thisone: function(){
                alert('you clicked this one');
            }
        };
    doModal();
});

So just to describe what I'm trying to do: I'm trying to make an easy way to create modal messages. Trying to reduce code repetition (I don't want to create dialogs for everything). So hopefully I'll be able to just define the title and content and buttons for a modal and then call a function (doModal()).

For some reason the code below does no pass the buttons correctly (no buttons are shown). If I simply substitute modal.buttons with this:

{
                thisone: function(){
                    alert('you clicked this one');
                }
            }

then it would work. But then I can't have an easy way of defining the buttons, which is against what I'm trying to do.

Here's the current code:

var modal = $('<div id="modal"><p></p></div>');
function doModal()
{
    var modal = $('<div id="modal"><p></p></div>');
    modal.text(modal.content);
    modal.attr('title', modal.title);
    modal.dialog('destroy');
    modal.dialog({
        modal:true,
        resizable:false,
        draggable:false,
        buttons: modal.buttons
    });
}

$(document).ready(function(){
    modal.title = 'Are you sure?';
    modal.content = 'Are you sure? Deleting a product cannot be undone.';
    modal.buttons = {
            thisone: function(){
                alert('you clicked this one');
            }
        };
    doModal();
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掐死时间 2024-10-13 11:54:14

您在全局和 doModal() 内部定义模态 2 次。

在 document.ready() 上调用的函数内部,您将按钮分配给全局范围内定义的模态。但在 doModal() 内部还存在本地定义的变量 modal,它不会分配按钮。

我将在 read() 函数中定义模态并将其作为参数传递给 doModal() :

function doModal(modal)
{ 
    modal.text(modal.content);
    modal.attr('title', modal.title);
    modal.dialog('destroy');
    modal.dialog({
        modal:true,
        resizable:false,
        draggable:false,
        buttons: modal.buttons
    });
}

$(document).ready(function(){
    var modal = $('<div id="modal"><p></p></div>');
    modal.title = 'Are you sure?';
    modal.content = 'Are you sure? Deleting a product cannot be undone.';
    modal.buttons = {
            thisone: function(){
                alert('you clicked this one');
            }
        };
    doModal(modal);
});

或者仅将动态选项传递给 doModal() :

function doModal(options)
{ 
    var modal = $('<div id="modal"><p></p></div>');
    modal.text(options.content);
    modal.attr('title', options.title);
    modal.dialog('destroy');
    modal.dialog({
        modal:true,
        resizable:false,
        draggable:false,
        buttons: options.buttons
    });
}

$(document).ready(function(){
    doModal(
            {
              title   :'Are you sure?',
              content :'Are you sure? Deleting a product cannot be undone.',
              buttons : {
                          thisone: function()
                                   {
                                      alert('you clicked this one');
                                   }
                        }
             }
           );
});

You define modal 2 times, global and inside doModal()

Inside the function you call on document.ready() you assign the buttons to the modal defined in global scope. But inside doModal() there also exists the local defined variable modal, which to you doesn't assign the buttons.

I would define modal inside the ready()-function and pass it as argument to doModal() :

function doModal(modal)
{ 
    modal.text(modal.content);
    modal.attr('title', modal.title);
    modal.dialog('destroy');
    modal.dialog({
        modal:true,
        resizable:false,
        draggable:false,
        buttons: modal.buttons
    });
}

$(document).ready(function(){
    var modal = $('<div id="modal"><p></p></div>');
    modal.title = 'Are you sure?';
    modal.content = 'Are you sure? Deleting a product cannot be undone.';
    modal.buttons = {
            thisone: function(){
                alert('you clicked this one');
            }
        };
    doModal(modal);
});

or pass only the dynamic options to doModal():

function doModal(options)
{ 
    var modal = $('<div id="modal"><p></p></div>');
    modal.text(options.content);
    modal.attr('title', options.title);
    modal.dialog('destroy');
    modal.dialog({
        modal:true,
        resizable:false,
        draggable:false,
        buttons: options.buttons
    });
}

$(document).ready(function(){
    doModal(
            {
              title   :'Are you sure?',
              content :'Are you sure? Deleting a product cannot be undone.',
              buttons : {
                          thisone: function()
                                   {
                                      alert('you clicked this one');
                                   }
                        }
             }
           );
});
坏尐絯 2024-10-13 11:54:14

我不知道这是否有帮助。我创建了一个显示模式对话框的“growl”jQuery 库。它没有按钮,但我认为你可以扩展它。这是图书馆的链接。

http://blog.bobcravens.com/2010/ 08/making-the-web-growl-using-jquery/

希望这能给您一些想法并且有帮助。

鲍勃

I don't know if this will help. I created a 'growl' jQuery library that displayed a modal dialog. It doesn't have the button, but I think you can extend it. Here is a link to the library.

http://blog.bobcravens.com/2010/08/making-the-web-growl-using-jquery/

Hopefully this gives you some ideas and is helpful.

Bob

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文