我有一个简单的html表单。当用户单击提交按钮时,将弹出一条消息以确认提交。如果用户单击“确定”,则应提交表单。如果用户单击“取消”,它将返回页面并且不执行任何操作。以下是我的提交按钮。
<input type="submit" name="" onclick="confirm('Are you sure to insert
data?');">
现在问题是即使用户点击取消也会提交表单。
请帮助。
答案 0 :(得分:5)
var target;
var guess_input_text;
var guess_input;
var finished = false;
var guesses = 0;
function do_game() {
var random_number = Math.random() * 100;
var random_number_integer = Math.floor(random_number);
target = random_number_integer;
while (!finished) {
guess_input_text = prompt("I am thinking of a number "+
"in the range 1 to 100.\n\n"+
"What is the number?");
guess_input = parseInt(guess_input_text);
guesses += 1;
finished = check_guess();
}
}
function check_guess() {
if (isNaN(guess_input)) {
alert("You have not entered a number.\n\n" +
"Please enter a number in the range 1 to 100.");
return false;
}
if ((guess_input < 1) || (guess_input > 100)) {
alert("Please enter an integer number in the range 1 to 100.");
return false;
}
if (guess_input > target) {
alert("Your number is too large!");
return false;
}
if (guess_input < target) {
alert("Your number is too small!");
return false;
}
alert("You got it! The number was " + target +
".\n\nIt took you " + guesses +
" guesses to get the number!");
return true;
}
语句仅评估表达式confirm()
或true
。要使其不提交表单,您需要返回值。
即,这将转化为:
false