<script type="text/javascript">
var validate = (function() {
var reClass = /(^|\s)required(\s|$)/; // Field is required
var reValue = /^\s*$/; // Match all whitespace
return function (form) {
var elements = form.elements;
var el;
var amount = document.forms["myForm"]["amount"].value;
var quantity = document.forms["myForm"]["quantity"].value;
for (var i=0, iLen=elements.length; i<iLen; i++) {
el = elements[i];
if (reClass.test(el.className) && reValue.test(el.value)) {
// Required field has no value or only whitespace
// Advise user to fix
alert('Please fix ' + el.name);
return false;
}
}
if (amount == null || amount == "" || isNaN(amount)) {
alert("Total Amount must be only numbers");
return false;
}
else if (quantity == null || quantity == "" || isNaN(quantity)) {
alert("Quantity must be only numbers");
return false;
}
else{
confirm("Confirm Order Submission ?");
}
}
}());
</script>
这是一个表单验证脚本,使用“必需”ID作为必填输入字段,工作正常。我只想在我使用confirm()尝试的最后一个if语句之后找到一个确认警告框,但即使我点击“取消”,表单仍然会被提交
答案 0 :(得分:2)
else{
if (confirm("Confirm Order Submission ?")) return true;
else return false;
}
这使它有效!
答案 1 :(得分:0)
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display a confirm box.</p>
<button onclick="myFunction()">Try it</button>
<script>
var x;
function myFunction() {
x = confirm("Press a button!");
alert(x)
if(x === true){
alert("Call Submit !!!!");
}else{
alert("Don't Call Submit !!!!");
}
}
</script>
</body>
</html>
&#13;
从w3school尝试此代码显示确认框
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_confirm
如果用户单击&#34;确定&#34;则confirm()方法返回true,否则返回false。
答案 2 :(得分:0)
请参阅https://www.w3schools.com/jsref/met_win_confirm.asp您可以使用 confirm()方法。
var txt;
var r = confirm("Press a button!");
if (r == true) {
txt = "You pressed OK!";
// Continue submit the form
} else {
txt = "You pressed Cancel!";
// Do nothing or return to form
}