prompt 消息對(duì)話框 不輸入、亂輸入,怎樣提示并恢復(fù)對(duì)話?
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>prompt</title>
? <script type="text/javascript">
? function rec(){
?? ?var score; //score變量,用來存儲(chǔ)用戶輸入的成績值。
?? ?score = prompt("輸入你的成績:");?????????????? ;
?? ?if(score>=90)
?? ?{
?? ??? document.write("你很棒!");
?? ?}
?? ?else if(score>=75)
??? {
?? ??? document.write("不錯(cuò)吆!");
?? ?}
?? ?else if(score>=60)
??? {
?? ??? document.write("要加油!");
??? }
?? else if(score= )? {}?? //我不輸入任何值or我亂輸入,怎樣提示用戶不要亂玩,并恢復(fù)對(duì)話?
??? else
?? ?{
?????? document.write("要努力了!");
?? ?}
? }
? </script>
</head>
<body>
??? <input name="button" type="button" onClick="rec()" value="點(diǎn)擊我,對(duì)成績做評(píng)價(jià)!" />
</body>
</html>
2016-11-18
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>prompt</title>
? <script type="text/javascript">
? function rec(){
var score; //score變量,用來存儲(chǔ)用戶輸入的成績值。
score = ?prompt("請(qǐng)輸入你的成績"); ? ? ? ? ? ? ?;
if(score>=90&&score<100)
{
? document.write("你很棒!");
}
else if(score>=75&&score<90)
? ? {
? document.write("不錯(cuò)吆!");
}
else if(score>=60&&score<75)
? ? {
? document.write("要加油!");
? ? }
? ? else if(score<60&&score>=0){
? ? ? ?document.write("要努力了!");
}
? ? else{
? ? ? ? document.write("你輸入的數(shù)字無效請(qǐng)刷新重試!");
? ? }
? }
? </script>
</head>
<body>
? ? <input name="button" type="button" onClick="rec()" value="點(diǎn)擊我,對(duì)成績做評(píng)價(jià)!" />
</body>
</html>
//這樣可以嗎?
2016-11-18
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>prompt</title>
? <script type="text/javascript">
? function rec(){
? ? var zhengcflag,score; //score變量,用來存儲(chǔ)用戶輸入的成績值。
? ? do
? ? { ??
? ? ? ? zhengcflag=true;
? ? ? ? score = prompt("輸入你的成績:"); ? ? ? ? ? ? ? ;
? ? ? ? if(score>=90)
? ? ? ? {
? ? ? ? ? ? document.write("你很棒!");
? ? ? ? }
? ? ? ? else if(score>=75)
? ? ? ? {
? ? ? ? document.write("不錯(cuò)吆!");
? ? ? ? }
? ? ? ? else if(score>=60)
? ? ? ? {
? ? ? ? ? ?document.write("要加油!");
? ? ? ? }
? ? ? ? else if(0<=score&&score<60)
? ? ? ? {
? ? ? ? ? ? document.write("要努力了!");
? ? ? ? }
? ? ? ? else?
? ? ? ? {
? ? ? ? ? ?zhengcflag=false;
? ? ? ? ? ?document.write("重新輸入!");
? ? ? ? }
? ? }
? ? while(zhengcflag==false)
? }
? </script>
</head>
<body>
? ? <input name="button" type="button" onClick="rec()" value="點(diǎn)擊我,對(duì)成績做評(píng)價(jià)!" />
</body>
</html>
設(shè)個(gè)標(biāo)志判斷是否正常輸入,剛剛寫的時(shí)候0<=score&&score<60忘了寫&&,把我瀏覽器弄崩潰了,無語
2016-11-18
var reg = new RegExp("^\\d+$");
if (!reg.test(score)) {
alert("請(qǐng)輸入數(shù)字");
rec();
return;
}
if (score.length >= 3) {
alert("請(qǐng)輸入正確數(shù)字");
rec();
return;
}