抱歉,我来晚了,但我会执行以下操作(假设只有两种可能的选择,即是或否,并且还假设正在使用模态类型确认框而不是花园品种警报/确认框):
将此变量存储表单添加到您的 html(表单隐藏输入的注意值未列出,因此当前为空值)
<form id="UserSelection" style="display:none"><input type="hidden" name="yes_or_no">
</form>
然后在模态确认框中添加两个按钮,其中按钮“yes”触发“yes”值,按钮“no”触发表单隐藏输入的“no”值:
<div id="ModalConfirm">
<button type="button" onclick="StoredSelectionVar('yes')">Yes</button>
<button type="button" onclick="StoredSelectionVar('no')">No</button>
</div>
其中函数 StoredSelectionVar() 定义为:
StoredSelectionVar(type) {
if (type === 'yes') {
document.forms['UserSelection']['yes_or_no'].value = 'yes';
} else {
document.forms['UserSelection']['yes_or_no'].value = 'no';
}
}
然后按如下方式修改剩余的javascript:
function YesOrNo() {
var x = document.forms['UserSelection']['yes_or_no'].value;
if (!x) {
window.setTimeout(YesOrNo, 1000);<!--Modify time as desired.....checks form
UserSelection to see if a value is present, which implies the user clicked either
the yes or no button for the hidden input, or no click if still a null value, in
which case the function is called again and the process repeated-->
} else {
if (x === 'yes') {
//Do This
} else {
//Do that
}
}
function yourFunction() {
if (msgbox("Do you want to continue")) {
YesOrNo();
} else {
//whatever
}
}