在JS中进行累加和,取文本框内的输入值。
<html><head><title>for循环实现1+2+3+····+n</title></head><body><formname='test'><h3>说明:</...
<html>
<head>
<title>for循环实现1+2+3+····+n</title>
</head>
<body>
<form name='test' >
<h3>说明:</h3>
<h5>在下面的文本框中填入任意一个数后回车<br>程序会计算出1累加到输入数的和</h5>
<input type=text id="myin" name='myin' onchange="judge()"><br>
</form>
</body>
</html>
<script type="text/JavaScript">
<!--
var Maxcounter,sum,i;
Maxcounter=0;
Maxcounter=document.getElementById('myin').value;
//Maxcounter=document.test.myin.value;
sum=0;
if((Maxcounter<0)||(Maxcounter>100))
{
alert("输入一个0~100之间的数");
}
else
{
for(i=0;i<=Maxcounter;i++)
{sum=sum+i;}
}
alert("从0加到"+Maxcounter+"的累加和是"+sum);
-->
</script>
Maxcounter=document.getElementById('myin').value;
Maxcounter=document.test.myin.value;
为什么这2个都不行呢? 展开
<head>
<title>for循环实现1+2+3+····+n</title>
</head>
<body>
<form name='test' >
<h3>说明:</h3>
<h5>在下面的文本框中填入任意一个数后回车<br>程序会计算出1累加到输入数的和</h5>
<input type=text id="myin" name='myin' onchange="judge()"><br>
</form>
</body>
</html>
<script type="text/JavaScript">
<!--
var Maxcounter,sum,i;
Maxcounter=0;
Maxcounter=document.getElementById('myin').value;
//Maxcounter=document.test.myin.value;
sum=0;
if((Maxcounter<0)||(Maxcounter>100))
{
alert("输入一个0~100之间的数");
}
else
{
for(i=0;i<=Maxcounter;i++)
{sum=sum+i;}
}
alert("从0加到"+Maxcounter+"的累加和是"+sum);
-->
</script>
Maxcounter=document.getElementById('myin').value;
Maxcounter=document.test.myin.value;
为什么这2个都不行呢? 展开
2个回答
展开全部
你没定义函数啊
<script type="text/JavaScript">
<!--
function judge(){
var Maxcounter,sum,i;
Maxcounter=0;
Maxcounter=document.getElementById('myin').value;
//Maxcounter=document.test.myin.value;
sum=0;
if((Maxcounter<0)||(Maxcounter>100))
{
alert("输入一个0~100之间的数");
}
else
{
for(i=0;i<=Maxcounter;i++)
{sum=sum+i;}
}
alert("从0加到"+Maxcounter+"的累加和是"+sum);
}
-->
</script>
<script type="text/JavaScript">
<!--
function judge(){
var Maxcounter,sum,i;
Maxcounter=0;
Maxcounter=document.getElementById('myin').value;
//Maxcounter=document.test.myin.value;
sum=0;
if((Maxcounter<0)||(Maxcounter>100))
{
alert("输入一个0~100之间的数");
}
else
{
for(i=0;i<=Maxcounter;i++)
{sum=sum+i;}
}
alert("从0加到"+Maxcounter+"的累加和是"+sum);
}
-->
</script>
展开全部
<script type="text/JavaScript">
<!--
function judge(){
var Maxcounter,sum,i;
Maxcounter=0;
Maxcounter=document.getElementById('myin').value;
//Maxcounter=document.test.myin.value;
sum=0;
if((Maxcounter<0)||(Maxcounter>100))
{
alert
}
else
{
for(i=0;i<=Maxcounter;i++)
{sum=sum+i;}
}
alert("从0加到"+Maxcounter+"的累加和是"+sum);
}
-->
</script>CAOZUO
<!--
function judge(){
var Maxcounter,sum,i;
Maxcounter=0;
Maxcounter=document.getElementById('myin').value;
//Maxcounter=document.test.myin.value;
sum=0;
if((Maxcounter<0)||(Maxcounter>100))
{
alert
}
else
{
for(i=0;i<=Maxcounter;i++)
{sum=sum+i;}
}
alert("从0加到"+Maxcounter+"的累加和是"+sum);
}
-->
</script>CAOZUO
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询