課程
/前端開發(fā)
/JavaScript
/JavaScript入門篇
怎么設定,如果輸入其他字符會報錯呢?就是文本框只能輸入0-100。
2018-12-11
源自:JavaScript入門篇 2-4
正在回答
再加一個判斷,你自己想一下這個判斷改怎么寫。。
<!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變量,用來存儲用戶輸入的成績值。
score = prompt("請輸入你的成績(0~100之間)");? ? ? ? ? ? ? ?;
if(score>=90 && score<=100)
{
? ?document.write("你很棒!");
}
else if(score>=75&&score<90)
? ? {
? ?document.write("不錯吆!");
else if(score>=60&&score<75)
? ?document.write("要加油!");
? ? }
??
else if(score>=0 && score<60)
? ? ? ?document.write("要努力了!");
?else
? ? ? ?document.write("您輸入的有誤,請重新輸入!")
? }
? </script>
</head>
<body>
? ? <input name="button" type="button" onClick="rec()" value="點擊我,對成績做評價!" />
</body>
</html>
小改了下
<script>
var score= prompt("請輸入")? ; //score變量,用來存儲用戶輸入的成績值。
if(isNaN(score)||score=="")
alert("input error!");
? ? ? ? rec();
? ? if(score>100||score<0)
? ? ? ?alert("input error!");
else if(score>=90)
else if(score>=75)?
else if(score>=60)
? ? else if(score<60&&score>=0)?
function rec(){
score =? ?prompt("請輸入成績:")? ? ;
var veget = /^(1|([1-9]\d{0,1})|100)$/;
???? if(!veget.test(score)){
???? ? ? alert("請輸入正確成績!");
???? }else{
???? ????if(score>=90){
???? ? ?????document.write("你很棒!");
???? ????}
???? ????else if(score>=75){
???? ? ?????document.write("不錯吆!");
???? ????else if(score>=60){
???? ????? ?document.write("要加油!");
????? ? }
????? ? else{
????? ? ? ?document.write("要努力了!");
???? }
舉報
JavaScript做為一名Web工程師的必備技術,本教程讓您快速入門
2 回答如何控制文本框里面只能輸入數字?
2 回答如何輸入框內在默認輸入文字?
2 回答如果輸入字母或漢字,不報錯,那么他們是如何與數字比較的
1 回答如果沒有在文本框中輸入值,點確定按鈕會有返回值嗎?如果有,返回值是什么呢?
2 回答prompt怎么設置輸入的值是0—100之間的正整數?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-12-12
再加一個判斷,你自己想一下這個判斷改怎么寫。。
2019-02-16
<!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變量,用來存儲用戶輸入的成績值。
score = prompt("請輸入你的成績(0~100之間)");? ? ? ? ? ? ? ?;
if(score>=90 && score<=100)
{
? ?document.write("你很棒!");
}
else if(score>=75&&score<90)
? ? {
? ?document.write("不錯吆!");
}
else if(score>=60&&score<75)
? ? {
? ?document.write("要加油!");
? ? }
??
else if(score>=0 && score<60)
? ? {
? ? ? ?document.write("要努力了!");
? ? }
?else
? ? {
? ? ? ?document.write("您輸入的有誤,請重新輸入!")
? ? }
? }
? </script>
</head>
<body>
? ? <input name="button" type="button" onClick="rec()" value="點擊我,對成績做評價!" />
</body>
</html>
2019-01-14
小改了下
<script>
? function rec(){
var score= prompt("請輸入")? ; //score變量,用來存儲用戶輸入的成績值。
if(isNaN(score)||score=="")
{
alert("input error!");
? ? ? ? rec();
}
? ? if(score>100||score<0)
? ? {
? ? ? ?alert("input error!");
? ? ? ? rec();
? ? }
else if(score>=90)
{
? ?document.write("你很棒!");
}
else if(score>=75)?
? ? {
? ?document.write("不錯吆!");
}
else if(score>=60)
? ? {
? ?document.write("要加油!");
? ? }
? ? else if(score<60&&score>=0)?
{
? ? ? ?document.write("要努力了!");
}
? }
? </script>
2019-01-04
function rec(){
var score; //score變量,用來存儲用戶輸入的成績值。
score =? ?prompt("請輸入成績:")? ? ;
var veget = /^(1|([1-9]\d{0,1})|100)$/;
???? if(!veget.test(score)){
???? ? ? alert("請輸入正確成績!");
???? }else{
???? ????if(score>=90){
???? ? ?????document.write("你很棒!");
???? ????}
???? ????else if(score>=75){
???? ? ?????document.write("不錯吆!");
???? ????}
???? ????else if(score>=60){
???? ????? ?document.write("要加油!");
????? ? }
????? ? else{
????? ? ? ?document.write("要努力了!");
???? ????}
???? }
? }