我正在尝试构建一个计算器,但是我无法执行求和操作,除了它连接发生了我如何能够做到这一点,还有如何在按下后在文本框中输入新值时清空文本框' ='我的代码就是这个
<title>Calculator</title>
<script>
var v = 0 ;
var operator = '';
function calc(obj){
if (obj.value == '+' || obj.value == '-' || obj.value == '*' || obj.value == '/' ){
v = document.getElementById("text_field").value;
operator = obj.value;
document.getElementById("text_field").value = '';
}
else if (obj.value == '='){
if (operator == '+')
{
document.getElementById("text_field"). value = v + document.getElementById("text_field").value ;
}
if (operator == '-')
{
document.getElementById("text_field").value = v - document.getElementById("text_field").value ;
}
if (operator == '*')
{
document.getElementById("text_field").value =v * document.getElementById("text_field").value ;
}
if (operator == '/')
{
document.getElementById("text_field").value = v / document.getElementById("text_field").value ;
}
}
else {
document.getElementById("text_field").value = document.getElementById("text_field").value + obj.value;
}
}
</script>
答案 0 :(得分:0)
使用parseInt:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseInt
例如:
parseInt(document.getElementById("text_field").value)