js小功能3:一个简单的计算器功能

html:

<input type='text' id='txt1' /> 
   <select id='select'>
        <option value='+'>+</option>
        <option value="-">-</option>
        <option value="*">*</option>
        <option value="/">/</option>
   </select>
   <input type='text' id='txt2' /> 
   <input type='button' value=' = ' onclick="count()"/> <!--通过 = 按钮来调用创建的函数,得到结果--> 
   <input type='text' id='fruit' /> 

js:

<script>
function count(){

var vtxt1=parseInt(document.getElementById('txt1').value);

var vtxt2=parseInt(document.getElementById('txt2').value);
var vselect=document.getElementById('select').value; 
var result='';
switch (vselect){
case '+':{
result
=vtxt1+vtxt2; break;
}
case '-':{
result
=vtxt1-vtxt2; break;
}
case '*':{
result
=vtxt1*vtxt2; break;
}
case '/':{
result
=vtxt1/vtxt2; break;
}
}
document.getElementById(
'fruit').value=result
}
</script>

 

posted @ 2019-07-18 10:17  彩色泡泡  阅读(274)  评论(0编辑  收藏  举报