谢谢楼上的请看下我的代码<script> window.onload = function(){ var sum = parseInt(document.getElementById("price").value) * parseInt(document.getElementById("num").value); var total = document.getElementById("total");
total.innerHTML = sum;
}</script>我只有把页面刷新一次 才显示出相加的结果
在文本框中作个onChange事件就可以了,然后赋值给sum就可以了。不用更新页面就可以实现功能。
你把代码放在window.onload中,只有页面加载完时才执行代码的 <html><head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>test</title> <script> function getTotal(){ if(document.getElementById("price").value=="")return; if(document.getElementById("num").value=="")return; var sum = parseInt(document.getElementById("price").value) * parseInt(document.getElementById("num").value); var total = document.getElementById("total"); total.innerHTML = sum; } </script> </head><body> 填完,光标离开文本框 <input type=text id=price value="" onblur="getTotal()"> <input type=text id=num value="" onblur="getTotal()"> <label id="total"></label></body></html>
window.onload = function(){ var sum = parseInt(document.getElementById("price").value) * parseInt(document.getElementById("num").value); var total = document.getElementById("total");
window.onload = function(){ var sum = parseInt(document.getElementById("price").value) * parseInt(document.getElementById("num").value);
var total = document.getElementById("total");
total.innerHTML = sum;
}</script>我只有把页面刷新一次 才显示出相加的结果
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>test</title>
<script>
function getTotal(){
if(document.getElementById("price").value=="")return;
if(document.getElementById("num").value=="")return;
var sum = parseInt(document.getElementById("price").value) * parseInt(document.getElementById("num").value);
var total = document.getElementById("total");
total.innerHTML = sum;
} </script>
</head><body>
填完,光标离开文本框
<input type=text id=price value="" onblur="getTotal()">
<input type=text id=num value="" onblur="getTotal()">
<label id="total"></label></body></html>
var total = document.getElementById("total");
if(document.getElementById("num").onchange){
total.innerHTML = sum;
}
}改成这样居然连显示都不显示了
$(document).ready(function() {
$("#Text3").bind("click",function (){
$("#Text3").attr("value",parseInt($("#Text1").attr("value"))+parseInt($("#Text2").attr("value")));
});
});
<input id="Text1" type="text" />
+
<input id="Text2" type="text" />
=
<input id="Text3" type="text" />
<input type="text" id="price" oninput="javascript:sum();" onpropertychange="javascript:sum();" />
<input type="text" id="num" oninput="javascript:sum();" onpropertychange="javascript:sum();" />
<input type="text" id="total" />
<script>
function sum(){
var price= document.getElementById("price").value;
var num= document.getElementById("num").value;
var sum = price * num;
sum= isNaN(sum)? 0:sum;
document.getElementById("total").value = sum;
} </script>