asp动态文本框相加
<formmethod="post"name="form"><scriptlanguage="javascript">functiontotal(){if(documen...
<form method="post" name="form">
<script language="javascript">
function total()
{
if(document.all.sl.length)
{
for(i=0;i<document.all.sl.length;i++)
{
var total;
total=parseFloat(document.all.sl[i].value) + parseFloat(document.all.sl[i].value);
document.all.zsl[i].value=total;
}
}
else
{
var total;
total=parseFloat(document.all.sl.value) + parseFloat(document.all.sl.value);
document.all.zsl.value=total;
}
}
</script>
<%
dim i
for i=1 to 5 %>
<input name="dj" type="text" id="dj" size="8" onkeyup="total();">
<input name="sl" type="text" id="sl" size="8" onkeyup="total();">
<input name="hj" type="text" id="hj" value="等待输入" size="8">
<br>
<%next%>
总数量:<input name="zsl" type="zsl" id="hj2">
</form>
----
以上代码请高手帮忙改改,总数量要得到上面所有sl文本框的总和。 展开
<script language="javascript">
function total()
{
if(document.all.sl.length)
{
for(i=0;i<document.all.sl.length;i++)
{
var total;
total=parseFloat(document.all.sl[i].value) + parseFloat(document.all.sl[i].value);
document.all.zsl[i].value=total;
}
}
else
{
var total;
total=parseFloat(document.all.sl.value) + parseFloat(document.all.sl.value);
document.all.zsl.value=total;
}
}
</script>
<%
dim i
for i=1 to 5 %>
<input name="dj" type="text" id="dj" size="8" onkeyup="total();">
<input name="sl" type="text" id="sl" size="8" onkeyup="total();">
<input name="hj" type="text" id="hj" value="等待输入" size="8">
<br>
<%next%>
总数量:<input name="zsl" type="zsl" id="hj2">
</form>
----
以上代码请高手帮忙改改,总数量要得到上面所有sl文本框的总和。 展开
2个回答
展开全部
获取元素用name属性,id必须是唯一的。
<html>
<body>
单价:<input name="dj" type="text" size="8">
数量:<input name="sl" type="text" size="8">
合计:<input name="hj" type="text" size="8" value="等待输入" readonly>
<br/>
单价:<input name="dj" type="text" size="8">
数量:<input name="sl" type="text" size="8">
合计:<input name="hj" type="text" size="8" value="等待输入" readonly>
<br/>
单价:<input name="dj" type="text" size="8">
数量:<input name="sl" type="text" size="8">
合计:<input name="hj" type="text" size="8" value="等待输入" readonly>
<br/>
总数量:<input type="text" name="zsl" id="hj2" readonly>
<br/>
总金额:<input type="text" name="zje" id="hj3" readonly>
<script language="javascript">
(function(){
var addEvent = function(e, t, f){
if(e.attachEvent){
e.attachEvent('on'+t,f);
}else if(e.addEventListener){
e.addEventListener(t,f,false);
}else{
e['on'+t] = f;
}
};
var form = {
dj : document.getElementsByName("dj") ,
sl : document.getElementsByName("sl") ,
hj : document.getElementsByName("hj") ,
tt : document.getElementById("hj2") ,
ss : document.getElementById("hj3") ,
calc : function(i){
var a = (/^\d+(\.\d+)?$/).test(this.dj[i].value) ? parseFloat(this.dj[i].value) : 0;
var b = (/^\d+(\.\d+)?$/).test(this.sl[i].value) ? parseFloat(this.sl[i].value) : 0;
this.hj[i].value = a * b;
var c = 0;
for(var j = 0; j < this.sl.length; j++){
c += (/^\d+(\.\d+)?$/).test(this.sl[j].value) ? parseFloat(this.sl[j].value) : 0;
}
this.tt.value = c;
var s = 0;
for(var j = 0; j < this.hj.length; j++){
s += (/^\d+(\.\d+)?$/).test(this.hj[j].value) ? parseFloat(this.hj[j].value) : 0;
}
this.ss.value = s;
},
bind : function(i){
var _this = this;
addEvent(this.dj[i], "keyup", function(){_this.calc(i);});
addEvent(this.sl[i], "keyup", function(){_this.calc(i);});
},
init : function(){
for(var i = 0; i < this.dj.length; i++){this.bind(i);}
}
}
form.init();
})();
</script>
</body>
</html>
<html>
<body>
单价:<input name="dj" type="text" size="8">
数量:<input name="sl" type="text" size="8">
合计:<input name="hj" type="text" size="8" value="等待输入" readonly>
<br/>
单价:<input name="dj" type="text" size="8">
数量:<input name="sl" type="text" size="8">
合计:<input name="hj" type="text" size="8" value="等待输入" readonly>
<br/>
单价:<input name="dj" type="text" size="8">
数量:<input name="sl" type="text" size="8">
合计:<input name="hj" type="text" size="8" value="等待输入" readonly>
<br/>
总数量:<input type="text" name="zsl" id="hj2" readonly>
<br/>
总金额:<input type="text" name="zje" id="hj3" readonly>
<script language="javascript">
(function(){
var addEvent = function(e, t, f){
if(e.attachEvent){
e.attachEvent('on'+t,f);
}else if(e.addEventListener){
e.addEventListener(t,f,false);
}else{
e['on'+t] = f;
}
};
var form = {
dj : document.getElementsByName("dj") ,
sl : document.getElementsByName("sl") ,
hj : document.getElementsByName("hj") ,
tt : document.getElementById("hj2") ,
ss : document.getElementById("hj3") ,
calc : function(i){
var a = (/^\d+(\.\d+)?$/).test(this.dj[i].value) ? parseFloat(this.dj[i].value) : 0;
var b = (/^\d+(\.\d+)?$/).test(this.sl[i].value) ? parseFloat(this.sl[i].value) : 0;
this.hj[i].value = a * b;
var c = 0;
for(var j = 0; j < this.sl.length; j++){
c += (/^\d+(\.\d+)?$/).test(this.sl[j].value) ? parseFloat(this.sl[j].value) : 0;
}
this.tt.value = c;
var s = 0;
for(var j = 0; j < this.hj.length; j++){
s += (/^\d+(\.\d+)?$/).test(this.hj[j].value) ? parseFloat(this.hj[j].value) : 0;
}
this.ss.value = s;
},
bind : function(i){
var _this = this;
addEvent(this.dj[i], "keyup", function(){_this.calc(i);});
addEvent(this.sl[i], "keyup", function(){_this.calc(i);});
},
init : function(){
for(var i = 0; i < this.dj.length; i++){this.bind(i);}
}
}
form.init();
})();
</script>
</body>
</html>
展开全部
<script>
function getAge(n){
if (n!="") {
bbb=document.getElementById("sl1").value
ccc=document.getElementById("sl2").value
ddd=document.getElementById("sl3").value
eee=document.getElementById("sl4").value
fff=document.getElementById("sl5").value
aaa=parseInt(bbb)+parseInt(ccc)+parseInt(ddd)+parseInt(eee)+parseInt(fff)
document.getElementById("zsl").value=aaa;
}else{
document.getElementById("zsl").value=""
}
}
</script>
<form method="post" name="form">
<%
dim i
for i=1 to 5 %>
<input name="dj" type="text" id="dj" size="8">
<input name="sl<%=i%>" type="text" id="sl" size="8" onKeyup="getAge(this.value)" value="0">
<input name="hj" type="text" id="hj" value="等待输入" size="8">
<br>
<%next%>
总数量:<input name="zsl" type="zsl" id="hj2">
</form>
function getAge(n){
if (n!="") {
bbb=document.getElementById("sl1").value
ccc=document.getElementById("sl2").value
ddd=document.getElementById("sl3").value
eee=document.getElementById("sl4").value
fff=document.getElementById("sl5").value
aaa=parseInt(bbb)+parseInt(ccc)+parseInt(ddd)+parseInt(eee)+parseInt(fff)
document.getElementById("zsl").value=aaa;
}else{
document.getElementById("zsl").value=""
}
}
</script>
<form method="post" name="form">
<%
dim i
for i=1 to 5 %>
<input name="dj" type="text" id="dj" size="8">
<input name="sl<%=i%>" type="text" id="sl" size="8" onKeyup="getAge(this.value)" value="0">
<input name="hj" type="text" id="hj" value="等待输入" size="8">
<br>
<%next%>
总数量:<input name="zsl" type="zsl" id="hj2">
</form>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询