我有一个包含各种选项的选择字段。当用户单击该字段来更改当前选择时,我需要启动一个提示,让用户确认他们希望继续更改,因为这将需要他们重做一个漫长的过程。如果他们取消更改,则需要防止所选选项发生更改,因为即使是快速的临时更改也会触发客户端上的自动保存。正因为如此,似乎其他解决方案 https://stackoverflow.com/questions/14019670/how-to-prevent-a-select-from-being-changed不起作用,因为它们保存原始值,让更改通过,然后在必要时恢复更改。
我不确定这是否是“正确的”方法,但我决定创建一个在每次单击选择字段时运行的函数。我通过使用本机成功解决了这个问题confirm
下面代码中的方法。我相信它有效,因为confirm
的同步性质允许我在任何事件侦听器收到更改之前恢复更改,因此基本上就像更改从未发生过(如果我错了,请纠正我)。不幸的是,我不被允许使用confirm
由于兼容性原因。
// This works, but I need to be able to do it without using 'confirm'
handlePotentialOptionChange(e){
if (this.currentOption !== e.target.value){
if (!confirm(`Are you sure you want to change the option from ${this.currentOption} to ${e.target.value}? You will be required to redo multiple fields.`)){
this.selectModel = this.currentOption // If user cancels the change, revert it to the original option.
}
}
}
由于我无法使用confirm
,我正在使用来自 Buefy 的对话框组件 https://buefy.org/documentation/dialog/。但是,它是异步运行的,这意味着当用户尝试选择不同的选项时,选项更改将在他们回答对话框之前完成。如果取消,下面的代码将恢复更改,但到那时已经太晚了,没有任何用处。
handlePotentialOptionChange(e){
if (this.currentOption !== e.target.value){
this.$dialog.confirm({
message: `Are you sure you want to change the option from ${this.currentOption} to ${e.target.value}? You will be required to redo multiple fields.`,
onConfirm: () => this.selectModel = e.target.value,
onCancel: () => this.selectModel = this.currentOption
})
}
}
是否可以让用户看到选项下拉列表,但禁用任何类型的选项更改,直到回答提示为止,以便我可以在异步内部相应地更改选项onConfirm
and onCancel
功能?或者我应该使用某种完全不同的方法?谢谢。