for 循环

该题主要是运用for 循环,以及 字符串转换成数字类型,数字相加求和

思路是:先获取到第一个和第二个输入框的值,然后使用parseInt将这两个值都转换成数字类型,

设置s即所有从fistValue到secondValue中间所有的值相加的和,最初的时候s=0;所以需要给s初始化为0;当secondvalue达到100的时候,不让再相加,否则一直随着i的增加,一直循环

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FXBTG-首页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<link href="css/base.css" rel="stylesheet" type="text/css">
<style type="text/css">
#sum{width:60px;height:30px;line-height:30px;background:red;}
</style>

<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(function() {
    
$("#sum").click(function(){
        var firstValue = parseInt($("#first").val());
        var secondValue = parseInt($("#second").val());    
        var s=0;
        for(var i=firstValue;i<= secondValue; i++){   
             s = s + i;         
        }
        
   $("#three").val(s);        
});
  
});
</script>
</head>
<body>

  
      
     <h1>现在需要算出第一个表单中的值到第二个表单中的值之间所有数字的和</h1>
     <input type="text" id="first" value=""/> <label>+</label><input type="text" id="second" value=""/> <div id="sum">=</div><input type="text" id="three" value=""/>
       

</body>
</html>
只要肯努力学习工作,面包会有的,牛奶也会有的
原文地址:https://www.cnblogs.com/sandraMaying/p/for_string.html