允许调用函数覆盖默认选项 - jQuery UI 对话框
我希望 CallingFunction 能够覆盖 showDivPopUp
函数中提供的默认选项。
function calling(){
showDivPopUp("title of pop up box", "message to show",
{
buttons:{
Yes: function () {
$(this).dialog("destroy");
},
No :function () {
$(this).dialog("destroy");
}
}
});
}
function showDivPopUp(title,msg,options){
var mgDiv = $("#msgDiv");
mgDiv.attr("innerHTML", msg);
return mgDiv.dialog({
modal: true,
buttons: {
Ok: function () {
$(this).dialog("destroy");
}
},
resizable: true,
show: "explode",
position: "center",
closeOnEscape: true,
draggable: false,
title : titl,
open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); }
});
}
因此,上面的代码应该显示两个按钮,即。 是
和否
,而不仅仅是确定
。我不想对每个选项都进行 if
检查。
更新:
在选项参数中,可能存在不应用默认值的选项。因此调用函数可能会指定 showDivPopUp
函数中未提及的 size
选项。
I want the callingFunction to be able to override the default options provided in the showDivPopUp
function.
function calling(){
showDivPopUp("title of pop up box", "message to show",
{
buttons:{
Yes: function () {
$(this).dialog("destroy");
},
No :function () {
$(this).dialog("destroy");
}
}
});
}
function showDivPopUp(title,msg,options){
var mgDiv = $("#msgDiv");
mgDiv.attr("innerHTML", msg);
return mgDiv.dialog({
modal: true,
buttons: {
Ok: function () {
$(this).dialog("destroy");
}
},
resizable: true,
show: "explode",
position: "center",
closeOnEscape: true,
draggable: false,
title : titl,
open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); }
});
}
So, the above code should show two buttons viz. Yes
and No
instead of just OK
. I don't want to do if
check for each option.
UPDATE:
In options parameter there might be options for which default is not applied. So the calling function may specify size
option which is not mentioned in the showDivPopUp
function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要使用 JQuery Extend() 方法将传递到函数中的选项与函数中指定的默认值合并。
看:
http://www.zachstronaut.com/posts/2009/05/ 14/javascript-default-options-pattern.html
和
http://api.jquery.com/jQuery.extend/
You want to use the JQuery extend() method to merge the options you pass into the function with the defaults that are specified within it.
See:
http://www.zachstronaut.com/posts/2009/05/14/javascript-default-options-pattern.html
and
http://api.jquery.com/jQuery.extend/
看起来“选项”是 JSON 格式。尝试省略 showDivPopUp 的第三个参数中的第一个
{buttons:
部分,或在 showDivPopUp 函数中设置buttons: options.buttons
。要对此进行扩展,请创建更多 json 对,并测试它们在 showDivPopUp 函数中是否存在。存在吗?覆盖。不存在吗?保留默认值。
通过以下方式访问每个:
使用以下方式测试是否存在:
对问题中的更新的响应:
使用 jquery.each 迭代传递选项中的所有元素。
Looks like 'options' is in JSON format. Try omitting the first
{buttons:
portion in the 3rd argument to showDivPopUp or setbuttons: options.buttons
in the showDivPopUp function.To expand on this, create more json pairs, and test for their existence in the showDivPopUp function. Exists? Override. Doesn't exist? Keep defaults.
Access each via:
Test for existence using:
Response to the update in the question:
Use jquery.each to iterate over all elements in the passed option.
在
mgDiv.dialog
函数中,修改buttons
键以具有条件值。例如:In your
mgDiv.dialog
function, modify thebuttons
key to have a conditional value. E.g.: