alert()、confirm()和prompt()的区别
标签:
文化 |
分类: javascript |
警告框:window.alert(str)或alert(str)
确认框:answer = window.confirm(str)或answer = confirm(str)
提示框:returnStr = window.prompt(targetQuestion,defaultString)
或prompt( targetQuestion,defaultString)
alert()和confirm()的最大区别是alert()没有返回值,confirm()的返回值有两种,一个是return false,一个是return true。当confirm()的对话框跳出时,点击确认是返回的值就是return true,点击取消时,返回的是return false
实例:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>confirm</title>
<script language="javascript">
var answer = prompt("算法运算题目:1+1 = ?");
if(answer == 2)
alert("\n算术运算结果:\n\n"+"恭喜您,你的答案正确!");
else if(answer == null)
alert("\n算术运算结果:\n\n"+"对不起,您还没作答!");
else
alert("\n算术运算结果:\n\n"+"对不起,您的答案错误!");
</script>
</head>
<body>
</body>
</html>
输出:
http://s8/middle/6436b8ect9bfd1f6d9557&690
当输入“2”时:
http://s16/middle/6436b8ect9bfd10c5ea4f&690
当输入为空时:
http://s15/middle/6436b8ect75ffb6ac0fce&690
当输入的不是“2”,也不为空时:
http://s11/middle/6436b8ect9bfd25dd837a&690
实例(学生信息采集系统)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sample</title>
<script language="javascript">
<!--
function CheckName(str){
var
nbool;
var
strLength = str.length;
if(strLength
> 3 && strLength
< 9)
nbool =
true;
else
nbool =
false;
return
nbool;
}
function CheckSex(sex){
var
nbool;
if(sex ==
"male" || sex == "female")
nbool =
true;
else
nbool =
false;
return
nbool;
}
function CheckNumber(num){
var
nbool;
var
numLength = num.length;
var index =
num.indexOf("2001");
if(numLength
!= 8 || index == -1)
nbool =
false;
else
nbool =
true;
return
nbool;
}
function CheckEmail(EmailAddr){
var
nbool;
var
strLength = EmailAddr.length;
var index1 =
EmailAddr.indexOf("@");
var index2 =
EmailAddr.indexOf(".",index1);
if(index1 ==
-1 || index2 == -1 || index1 == 0 || index2 == strLength-1)
nbool =
false;
else
nbool =
true;
return
nbool;
}
function MyMain(){
var MyName =
prompt("姓名:(4到8个字符)");
var nName =
CheckName(MyName);
while(nName
== false){
var NameMsg
= "姓名字段验证:\n\n";
NameMsg +=
"结果:格式错误.\n";
NameMsg +=
"格式:长度必须为4到8个字符.\n";
NameMsg +=
"处理:单击''确定''按钮返回修改.\n";
alert(NameMsg);
MyName =
prompt("姓名:(4到8个字符)");
nName =
CheckName(MyName);
}
var MySex =
prompt("性别:(male 或 female)","male");
var nSex =
CheckSex(MySex);
while(nSex
== false){
var SexMsg =
"性别字段验证:\n\n";
SexMsg +=
"结果:格式错误。\n";
SexMsg +=
"格式:必须为''male''或''famale''.\n";
SexMsg +=
"处理:单击''确定''按钮返回修改。\n";
alert(SexMsg);
MySex =
prompt("性别;(male或female)","male");
nSex =
CheckSex(MySex);
}
var MyNum =
prompt("学号:(形如 2001****)","2001");
var nNum =
CheckNumber(MyNum);
while(nNum
== false){
var NumMsg =
"学号字段验证:\n\n";
确认框:answer = window.confirm(str)或answer = confirm(str)
提示框:returnStr = window.prompt(targetQuestion,defaultString)
alert()和confirm()的最大区别是alert()没有返回值,confirm()的返回值有两种,一个是return false,一个是return true。当confirm()的对话框跳出时,点击确认是返回的值就是return true,点击取消时,返回的是return false
实例:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>confirm</title>
<script language="javascript">
var answer = prompt("算法运算题目:1+1 = ?");
if(answer == 2)
else if(answer == null)
else
</script>
</head>
<body>
</body>
</html>
输出:
http://s8/middle/6436b8ect9bfd1f6d9557&690
当输入“2”时:
http://s16/middle/6436b8ect9bfd10c5ea4f&690
当输入为空时:
http://s15/middle/6436b8ect75ffb6ac0fce&690
当输入的不是“2”,也不为空时:
http://s11/middle/6436b8ect9bfd25dd837a&690
实例(学生信息采集系统)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sample</title>
<script language="javascript">
<!--
function CheckName(str){
}
function CheckSex(sex){
}
function CheckNumber(num){
}
function CheckEmail(EmailAddr){
}
function MyMain(){

加载中…