html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用switch做一个查询结果</title>
<script src="js/switch.js"></script>
</head>
<body>
<input type="text" id="txt"/>
<input type="button" id="btn" value="点击我查询水果价钱"/>
</body>
</html>
js
window.onload = function() {
var txt = document.getElementById("txt");
var btn = document.getElementById("btn");
btn.onclick = function() {
var val = txt.value;
switch (val) {
case "苹果":
alert("苹果的价格是:5元/斤");
break;
case "梨子":
alert("梨子的价格是:8元/斤");
case "香蕉":
alert("香蕉的价格是:1.8元/斤");
break;
default:
alert("今天没进货");
}
}
}
效果图