Javascript:要求输入数值,给出不同的答案
快速更新强> 我可能已经修好了。现在代码如下所示:Javascript:要求输入数值,给出不同的答案,javascript,if-statement,Javascript,If Statement,快速更新 我可能已经修好了。现在代码如下所示: <script type="text/javascript"> function rangeAnswers() { var answer = prompt ("How much do you like pizza?") if (answer > 0 ) confirm("That's a bit low") window.location="http://w
<script type="text/javascript">
function rangeAnswers() {
var answer = prompt ("How much do you like pizza?")
if (answer > 0 )
confirm("That's a bit low")
window.location="http://www.continue.com";
if (answer > 20 )
confirm("Quite a decent score, isn't it?")
window.location="http://www.continue.com";
}
</script>
t没有加载,因为你有一个尾随的else,你对答案有偏执,但对整个布尔人没有。尝试:
<script type="text/javascript">
function rangeAnswers(){
var answer = prompt ("How much do you like pizza?");
if (answer > 0) {
alert("Nice!");
window.location.href="http://www.continue.com";
}else if (answer > 20){
alert("Nice!");
window.location.href="http://www.continue.com";
}
}
</script>
函数rangeAnswers(){
var answer=prompt(“你喜欢多少比萨饼?”);
如果(答案>0){
警觉(“好极了!”);
window.location.href=”http://www.continue.com";
}否则,如果(回答>20){
警觉(“好极了!”);
window.location.href=”http://www.continue.com";
}
}
已编辑-完整版本:
<script type="text/javascript">
function rangeAnswers(){
var answer,
bad=false;
while(true){
answer = prompt('How much do you like pizza?');
if(isNaN(answer)){
bad=true;
}else{
bad=false;
answer = parseInt(answer,10);
if(answer>100 || answer<0){
bad=true;
}else if(answer===100){
alert('Wow, you REALLY love pizza!');
}else if (answer >= 75) {
alert('75-99!');
}else if (answer >= 50){
alert('50-74');
}else if (answer >= 25){
alert('25-49');
}else{
alert('0-24');
}
if(!bad){
window.location.href="http://www.continue.com";
return;
}
}
if(bad){
alert('I need a number between 0 and 100!');
}
}
}
</script>
函数rangeAnswers(){
答案是,
坏=假;
while(true){
回答=提示(“你喜欢多少比萨饼?”);
如果(isNaN(回答)){
坏=真;
}否则{
坏=假;
答案=parseInt(答案,10);
如果(答案>100 | |答案=75){
警报('75-99!');
}否则,如果(答案>=50){
警报('50-74');
}否则,如果(答案>=25){
警报('25-49');
}否则{
警报('0-24');
}
如果(!坏){
window.location.href=”http://www.continue.com";
返回;
}
}
如果(坏){
警报('我需要一个介于0和100之间的数字!');
}
}
}
您的html应该如下所示
<head>
<title>Title</title>
<script src="file.js" type="text/javascript"></script>
</head>
<body>
<h2><a href="javascript:rangeAnswers();">Click here to tell us how much you like pizza and continue!</a></h2>
</body>
</html>
标题
在您的代码中尝试这一点
function rangeAnswers() {
var answer = prompt ("How much do you like pizza?")
if (answer<=25 && answer =>0){
alert("That's a bit low")//you are not alerting your data properly.
window.location.href="http://www.continue.com";//use window.location.href instead of window.location
}
else if (answer<=50 && answer =>25){
alert("You don't like it nor hate it")
window.location.href="http://www.continue.com";
}
}
函数rangeAnswers(){
var answer=prompt(“您喜欢多少比萨饼?”)
如果(回答0){
警报(“有点低”)//您没有正确地警报数据。
window.location.href=”http://www.continue.com“;//使用window.location.href而不是window.location
}
如果有的话(回答25){
警惕(“你既不喜欢也不讨厌它”)
window.location.href=”http://www.continue.com";
}
}
更新:
执行相同操作的其他方法是使用开关案例:
function rangeAnswers() {
var answer = prompt ("How much do you like pizza?")
switch(true){
case answer<=25 && answer =>0:
alert("That's a bit low")//you are not alerting your data properly.
window.location.href="http://www.continue.com";//use window.location.href instead of window.location
break;
case answer<=50 && answer =>25:
alert("You don't like it nor hate it")
window.location.href="http://www.continue.com";
break;
}
}
函数rangeAnswers(){
var answer=prompt(“您喜欢多少比萨饼?”)
开关(真){
案例解答0:
警报(“有点低”)//您没有正确地警报数据。
window.location.href=”http://www.continue.com“;//使用window.location.href而不是window.location
打破
个案解答25:
警惕(“你既不喜欢也不讨厌它”)
window.location.href=”http://www.continue.com";
打破
}
}
我认为不正确的语法使您的流程无法进行:
<script type="text/javascript">
function rangeAnswers() {
var answer = prompt ("How much do you like pizza?");
answer = answer - 0;
if(isNaN(answer))
{
alert("The number you specified is invalid");
}else if (answer >= 0 && answer <=25)
{
alert("You don't like it very much");
window.location="http://www.continue.com";
}else if (answer > 25 && answer <=50)
{
alert("Not good but not bad");
window.location="http://www.continue.com";
}
//and so on ....
}
</script>
函数rangeAnswers(){
var answer=prompt(“你喜欢多少比萨饼?”);
答案=答案-0;
如果(isNaN(回答))
{
警报(“您指定的号码无效”);
}否则如果(答案>=0&&answer 25&&answer)请添加您的所有源代码…或者这就是您的全部?尝试在浏览器中打开开发者控制台,如firefox中的firebug或chrome中的开发者工具…使用浏览器中的控制台查找并修复所有语法错误,例如if(答案>0)
而不是if(答案)>0
我已经用调试器纠正了语法错误,因为正如我所说,我是js新手。是的,这就是我所得到的。这是一个测试页面,所以我只有带标题的h1和带链接的h2,还有一个不起作用的小函数。添加一些关于文件结构的信息……谢谢。我实际上在这里修复了代码d。它可以工作,但不显示我的警报/提示!要获取警报,您需要alert('text');
,因为这是一个函数调用,函数使用这些偏执。现在它可以工作了,谢谢!我用最后一个问题将代码更新到顶部,这是针对不正确的值(小于零,大于100)。该开关语句不正确。answer=0
如果答案为true
,则仅等于answer
。您的“大于或等于”运算符不正确,如下所示:well@JanDvorak:您不能期望问题的代码正确。切换(true)
而不是切换(答案)
将生成功能代码。但是,我不确定是否喜欢这种模式。让我们
function rangeAnswers() {
var answer = prompt ("How much do you like pizza?")
if (answer<=25 && answer =>0){
alert("That's a bit low")//you are not alerting your data properly.
window.location.href="http://www.continue.com";//use window.location.href instead of window.location
}
else if (answer<=50 && answer =>25){
alert("You don't like it nor hate it")
window.location.href="http://www.continue.com";
}
}
function rangeAnswers() {
var answer = prompt ("How much do you like pizza?")
switch(true){
case answer<=25 && answer =>0:
alert("That's a bit low")//you are not alerting your data properly.
window.location.href="http://www.continue.com";//use window.location.href instead of window.location
break;
case answer<=50 && answer =>25:
alert("You don't like it nor hate it")
window.location.href="http://www.continue.com";
break;
}
}
<script type="text/javascript">
function rangeAnswers() {
var answer = prompt ("How much do you like pizza?");
answer = answer - 0;
if(isNaN(answer))
{
alert("The number you specified is invalid");
}else if (answer >= 0 && answer <=25)
{
alert("You don't like it very much");
window.location="http://www.continue.com";
}else if (answer > 25 && answer <=50)
{
alert("Not good but not bad");
window.location="http://www.continue.com";
}
//and so on ....
}
</script>