标签:val tns function head core back 表达 for blank
在一些高级语言如C#中,switch分支的值只能是常量,而js中可以是变量或表达式:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $(‘#btnScore‘).bind(‘click‘,function(){ var score = $(‘#txtScore‘).val(); var reg = /^[0-9]+.?[0-9]*$/ if (!reg.test(score)) { alert(‘请输入合法的数字!‘); return false; } calcScore(score); }) }); function calcScore(score){ switch(true){ //不能写switch(score),因为表达式值为bool类型 case score > 90 && score <= 100: alert(‘优‘); break; case score > 80 && score <= 90: alert(‘良‘); break; case score > 59 && score <= 80: alert(‘及格‘); break; case score > -1 && score <= 59: alert(‘不及格‘); break; default: alert(‘输入分值范围错误!‘); break; } } </script> </head> <body> <label for=‘txtScore‘>分值:</label><input id=‘txtScore‘ type=‘text‘> <input id=‘btnScore‘ type=‘button‘ value=‘评分‘ /> </body> </html>
标签:val tns function head core back 表达 for blank
原文地址:http://www.cnblogs.com/yaotome/p/7222018.html