我有这个相对简单的html / javascript文档,使用2种可能的结果(仅使用>或<),我试图将其扩展为8个选项。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fielder's Choice Spinner</title>
<meta name="description" content="Instore spinner for FC">
<meta name="author" content="Matthew Davis">
<link href="style.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--ill just leave this here-->
<script language="JavaScript">
var response = (function getRandomIntInclusive(min, max) {
min = Math.ceil(1);
max = Math.floor(1000);
return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
})();
document.write(response)
function script() {
if (response == 1000) {
window.location.replace("freeorder.html")
}
if (999 >= response >= 950){
window.location.replace("15offorder/15offorder.html")
}
if (949 >= response >= 849){
window.location.replace("20offnext.html")
}
if (848 >= response >= 698){
window.location.replace("keychain.html")
}
if (697 >= response >= 497){
window.location.replace("freebaseball.html")
}
if (496 >= response >= 396){
window.location.replace("nospin1.html")
}
if (395 >= response >= 295){
window.location.replace("nospin2.html")
}
if (294 >= response >= 1){
window.location.replace("stamp.html")
}
}
</script>
</head>
<body>
<button id="link">Link</button>
<script>
document.getElementById('link').onclick = function () {
script();
};
</script>
</body>
</html>
但是,现在按钮仅在达到 stamp.html 的条件并按下按钮时重定向。所有其他结果按钮什么都不做。浏览器开发人员控制台中没有错误。这是'if'语句或其他问题吗?
干杯,马特
答案 0 :(得分:1)
这是你的问题:
294 >= response >= 1
评估为:
(294 >= response) >= 1
因此:
resp = 10
(294 >= 10) == true
true >= 1 // this evaluates to true
但这对你的其他if语句不起作用:
999 >= response >= 950
response = 960
999 >= 960 === true
true >= 950 === false
因此永远不会落入这个if语句。
您需要进行2次明确检查:
if (999 >= response && response >= 950)