日期:2014-05-16  浏览次数:20418 次

简单的javascript哪里出错?
<html>
<head>
<title> 标题 </title>
<script language="javascript">

  function a()
  {
  Confirm("是否进行运算!");
  var prompt1 = Prompt("请输入一个正整数 n :","50");
  if(prompt1 < 0 ){ alert("输入有误!");}
  if(prompt1 > 0 ){ b(prompt1);}
  }

  function b(n)
  {

  for (int i =0 ; i <= n ; i++)
  {
  var res = 0;
  var ii = i;
  while(ii%2==0)
  {
  res += i;
  ii += 1;
  }
  }
  document.form.text.value = res ;
  }
}
</script>

</head>
<body>

<form action = "" method = "post" name="form">
<input type="text" name = "text" value = "0" disabled = disabled />
<input type="button" value = "正整数n内的偶数和" name = "button" onClick = "a()"/>
</form>

</body>
</html>

------解决方案--------------------
HTML code

<html>
<head>
<title> 标题 </title>
<script language="javascript">

  function a()
  {
  confirm("是否进行运算!");
  var prompt1 = prompt("请输入一个正整数 n :","50");
  if(prompt1 < 0 ){ alert("输入有误!");}
  if(prompt1 > 0 ){ b(prompt1);}
  }

  function b(n)
  {

  for (var i =0 ; i <= n ; i++)
  {
  var res = 0;
  var ii = i;
  while(ii%2==0)
  {
  res += i;
  ii += 1;
  
  }
  document.form.text.value = res ;
  }
}
</script>

</head>
<body>

<form action = "" method = "post" name="form">
<input type="text" name = "text" value = "0" disabled = disabled />
<input type="button" value = "正整数n内的偶数和" name = "button" onClick = "a()"/>
</form>

</body>
</html>

------解决方案--------------------
Confirm、Prompt 注意大小写

js中变量 用 var来定义
------解决方案--------------------
错得多得去了。。
confirm、prompt 注意大小写
js没有int关键字
逻辑错误
HTML code
<!DOCTYPE html>
<html>
<head>
    <title> 标题 </title>
    <meta charset="utf-8"/>
    <script language="javascript">
    
        function a(){
            confirm("是否进行运算!");
            var prompt1 = prompt("请输入一个正整数 n :","50");
            if(prompt1 < 0 ){ alert("输入有误!");}
            if(prompt1 > 0 ){ b(prompt1);}
        }
    
        function b(n){
            var res = 0;
            for (var i =0 ; i <= n ; i++){
                res += i % 2 === 0 && i;
            }
            document.form.text.value = res ;
        }
    
    </script>

</head>
<body>

    <form action = "" method = "post" name="form">
        <input type="text" name = "text" value = "0" disabled = disabled />
        <input type="button" value = "正整数n内的偶数和" name = "button" onClick = "a()"/>
    </form>

</body>
</html>

------解决方案--------------------
错得多得去了。。
1. confirm、prompt 注意大小写
2. js没有int关键字
3. 逻辑错误
HTML code
<!DOCTYPE html>
<html>
<head>
    <title> 标题 </title>
    <meta charset="utf-8"/>
    <script type="text/javascript">
        fun