JS在文本框中输入数据后,自动计算总合,并且不刷新页面的代码
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题</title>
<script type="text/javascript">
function sum(obj) {
var z = document.getElementById("z");
var a = document.getElementById("a");
var b = document.getElementById("b");
var c = document.getElementById("c");
all.value=parseInt(z.value);
if(a.value!='')
{
y.value=parseInt(a.value);
h.value=parseInt(z.value)-parseInt(a.value);
}
if(a.value!=''&&b.value!='')
{
y.value=parseInt(b.value)+parseInt(a.value);
h.value=parseInt(z.value)-parseInt(a.value)-parseInt(b.value);
}
if(a.value!=''&&b.value!=''&&c.value!='')
{
y.value=parseInt(b.value)+parseInt(a.value)+parseInt(c.value);
h.value=parseInt(z.value)-parseInt(a.value)-parseInt(b.value)-parseInt(c.value);
}
}
</script>
</head>
<body>
总分:<input type="text" id="z" οnkeyup="sum(this);" />
<input type="text" id="a" οnkeyup="sum(this);" />
<input type="text" id="b" οnkeyup="sum(this);" />
<input type="text" id="c" οnkeyup="sum(this);" />
总分:<input type='text' id='all' style="border:0px solid white; width:25px" />
已选:<input type='text' id='y' style="border:0px solid white; width:25px" />
还剩:<input type='text' id='h' style="border:0px solid white; width:25px" />
</body>
</html>
扩展资料
js实现input的赋值
<input id="name1" name="teacherName" type="text" />
$('#name1').val('值');
document.getElementById('name1').value='值';
document.getElementById('name1').html('值');
document.getElementById('name1').attr('值');
document.getElementById('name1').innerText = '值';
<input class="easyui-textbox" id="name" name="teacherName" type="text" />
$('#name').textbox('setValue','值');
var val = $("#name").textbox('getValue')
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题</title>
<script type="text/javascript">
function sum(obj) {
var a = document.getElementById("a");
var b = document.getElementById("b");
var s = document.getElementById("sum");
if(a.value === "" || b.value === "") {
return;
}
s.value = parseInt(a.value) + parseInt(b.value);
}
</script>
</head>
<body>
<input type="text" id="a" onkeyup="sum(this);" />
<input type="text" id="b" onkeyup="sum(this);" />
<input type="text" id="sum" />
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<script>
getEl = function(name){
return document.getElementById(name);
}
function getSum(){
getEl('result').value = parseInt(getEl('one').value) + parseInt(getEl('two').value);
}
</script>
</head>
<body>
<input type="number" id="one" value="0" onkeyup="getSum()">
<input type="number" id="two" value="0" onkeyup="getSum()">
<input type="text" id="result" value="0" readOnly="true">
</body>
</html>
能写下具体的代码么 我JS很菜
楼下哥们写的很好啊