PHP和脚本的新手,遇到了一个我无法解决的问题。
我有一个预订页面,用户勾选复选框以预订时间。然后,提交按钮会运行一个脚本来检查他们是否已经预订了他们要求的次数。因此,如果要求3个预订时间并且他们只选择2则会触发警报窗口。按OK后,窗口应重新加载,以便他们可以再次选择。这在Firefox中很有用,并且会重新加载所有选中的复选框。
但是,如果你在IE和Chrome中执行此操作,警告框会劫持页面,窗口仍然会选中复选框。
function CheckBox() {
try {
var max = document.mainForm.serNo.length;
var count = 0;
var tot = <?php echo $Num ?>;
for (var i = 0; i < max; i++) {
if (document.mainForm.serNo[i].checked == true) {
count++;
serNoChecked = i;
}
}
if (count < tot) {
for (var i = 0; i < max; i++) {
window.alert('THERE IS A PROBLEM WITH YOUR BOOKING. \n You have asked to book ' + tot + ' times, but have only selected ' + count + ' times.\n Please re-select the same number of times as asked at the start or re-start your booking');
window.location.assign("url");
}
}
} catch (e) {
alert(e.message);
}
}
答案 0 :(得分:0)
你有一个循环,弹出多次,这是多余的。
还建议使用location.reload(true)
,因为true
参数从服务器而不是从缓存重新加载当前页面。
if(!alert("your text")){}
检查警报框是否未关闭,关闭时是否执行window.reload(true)
提交按钮和表单必须具有唯一ID。
我已在下面发布了更新的代码:
document.getElementById("mySubmitButtonId").addEventListener("click", function(event) {
try {
event.preventDefault();
var max = document.mainForm.serNo.length;
var count = 0;
var tot = <?php echo $Num ?>;
for (var i = 0; i < max; i++) {
if (document.mainForm.serNo[i].checked == true) {
count++;
serNoChecked = i;
}
}
if (count !== tot) {
if(!alert('THERE IS A PROBLEM WITH YOUR BOOKING. \n You have asked to book ' + tot + ' times, but have only selected ' + count + ' times.\n Please re-select the same number of times as asked at the start or re-start your booking')){
window.location.reload(true);
}
} else {
document.getElementById("myFormId").submit();
}
} catch (e) {
alert(e.message);
}
});