jquery - 动态定义选项
所以只是为了描述我正在尝试做的事情:我正在尝试创建一种简单的方法来创建模式消息。尝试减少代码重复(我不想为所有内容创建对话框)。因此,希望我能够定义模态的标题、内容和按钮,然后调用函数(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在全局和 doModal() 内部定义模态 2 次。
在 document.ready() 上调用的函数内部,您将按钮分配给全局范围内定义的模态。但在 doModal() 内部还存在本地定义的变量 modal,它不会分配按钮。
我将在 read() 函数中定义模态并将其作为参数传递给 doModal() :
或者仅将动态选项传递给 doModal() :
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() :
or pass only the dynamic options to doModal():
我不知道这是否有帮助。我创建了一个显示模式对话框的“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