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>