我在重用对话框时遇到问题,这样我就不必重复代码。我遇到的问题是,当我单击“添加新”按钮时,我有“保存”和“取消”按钮。对话框中的保存按钮负责保存信息。如果我单击“编辑现有”按钮来编辑我的信息,我希望“保存”按钮更改为“编辑”按钮。这可能吗?
最佳答案
我认为您想要做的是,在运行时,根据用户操作更改按钮的文本。这个discussion理查德·沃斯的参与可能会有所帮助,这也是我为了完成你想要做的事情而必须使用的。
基本上,通过将按钮数组定义为对象文字(在对话框定义中内联),您尝试做的事情是不可能的。您应该做的是在对话框初始化之外定义按钮数组;您的数组索引可以是您的标签文本(正如您将在消息讨论示例中看到的那样)。然后,当您初始化数组时,只需将 Buttons 属性分配给 Buttons 数组即可。
在伪代码中,您将执行以下操作:
function createDialog(mode) { //mode=new for dayClick; edit for eventClick
var myButtons = {}; //initialize the object to hold my buttons
if (mode === "new") {
myButtons["Save"] = function() {...} //the function that does the save
} else {
myButtons["Edit"] = function() {...} //the function that edits
}
myButtons["Cancel"] = function() {...} //common cancel function for either new or edit mode
//other logic
$("#dialogAddEdit").dialog({
...
buttons: myButtons, //assign the buttons property to your myButtons array object
...
});
}
希望这有帮助!!
关于jquery - 更改 jquery 对话框按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3498776/