<form id=orders name="orders" action="orders_action.php" method="post" enctype="multipart/form-data" onsubmit="onTimeChange();
">
如果时间在正确范围内,提交表单时,应该成功提交表单,如果表单不在正确的范围内,则应使用alert box(already done by using onTimeChange() function)
给出警告消息。我面临的问题是即使出现警告框,我按下“确定”按钮,表单数据也会提交到数据库中!我怎么能阻止它?
var input = document.getElementById('time');
function onTimeChange() {
var minTime = input.min;
var maxTime = input.max
var value = input.value + ':00'
console.log(minTime,maxTime,value);
if(minTime > value || value > maxTime) {
alert('Pharmacy is opened only between 8AM and 4PM!')
}
else{
return false;
}
}
这是onTimeChange()
的代码答案 0 :(得分:0)
你必须使用&#39; preventDefault&#39;提交事件的方法。
function onTimeChange(event) {
var minTime = input.min;
var maxTime = input.max
var value = input.value + ':00'
console.log(minTime,maxTime,value);
if (minTime > value || value > maxTime) {
alert('Pharmacy is opened only between 8AM and 4PM!')
} else {
event.preventDefault();
}
}
答案 1 :(得分:-1)
你可以这样尝试
<form id=orders name="orders" action="orders_action.php" method="post"
enctype="multipart/form-data" onsubmit="return onTimeChange();">
function onTimeChange() {
var minTime = input.min;
var maxTime = input.max
var value = input.value + ':00'
console.log(minTime,maxTime,value);
if(minTime > value || value > maxTime) {
alert('Pharmacy is opened only between 8AM and 4PM!');
return false;
}
else{
return true;
}
}