我先用ASP双层循环出一组input,我想用js来改变这一组input中某些满足条件的值,要改变input值的想法如图上所示,就是找到满足条件的值后,纵向求和后再赋值给它。下面的代码我只是能找到满足条件的值,不知道如何就和。大家注意jS中“== 0.2”中的0.2不是固定值。我是方便大家理解我的意图。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<%
For N=0 To 3
Response.Write "<tr>"
For I=0 To 3
Response.Write "<td><input name='h"&N&"k"&I&"' id='h"&N&"k"&I&"' type=text value='"&N&"."&I&"' /></td>"
Next
Response.Write "</tr>"
Next
%>
</table>
<script language="javascript">
for (JsZN = 0; JsZN < <%=N%>; JsZN++) {
for (JsZI = 0; JsZI < <%=I%>; JsZI++) {
if (document.getElementById("h"+JsZN+"k"+JsZI).value == 0.2)
document.getElementById("h"+JsZN+"k"+JsZI).value = 这一列数值的和;
}
}
</script>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<%
For N=0 To 3
Response.Write "<tr>"
For I=0 To 3
Response.Write "<td><input name='h"&N&"k"&I&"' id='h"&N&"k"&I&"' type=text value='"&N&"."&I&"' /></td>"
Next
Response.Write "</tr>"
Next
%>
</table>
<script language="javascript">
for (JsZN = 0; JsZN < <%=N%>; JsZN++) {
for (JsZI = 0; JsZI < <%=I%>; JsZI++) {
if (document.getElementById("h"+JsZN+"k"+JsZI).value == 0.2)
document.getElementById("h"+JsZN+"k"+JsZI).value = 这一列数值的和;
}
}
</script>
</body>
</html>
for (JsZN = 0; JsZN < <%=N%>; JsZN++) {
var total = 0;
for (JsZI = <%=I%>; JsZI >= 0 ; JsZI--) {
total += document.getElementById("h"+JsZN+"k"+JsZI).value;
if (JsZI==0)
document.getElementById("h"+JsZN+"k"+JsZI).value = total;
}
}
如果赋值给第几个未知,可以把符合条件的input的JsZN、JsZI记录下来,求和好了再赋值 for (JsZN = 0; JsZN < <%=N%>; JsZN++) {
var total = 0;
var h,k = -1;
for (JsZI = <%=I%>; JsZI >= 0 ; JsZI--) {
total += document.getElementById("h"+JsZN+"k"+JsZI).value;
if(符合条件){
h = JsZN;
k = JsZI;
}
}
if(h!=-1){
document.getElementById("h"+h+"k"+k).value = total;
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input name='h0k0' id='h0k0' type=text value='0.0' /></td>
<td><input name='h0k1' id='h0k1' type=text value='0.1' /></td>
<td><input name='h0k2' id='h0k2' type=text value='0.2' /></td>
<td><input name='h0k3' id='h0k3' type=text value='0.3' /></td>
<td><input name='h0k4' id='h0k4' type=text value='0.4' /></td>
<td><input name='h0k5' id='h0k5' type=text value='0.5' /></td>
</tr>
<tr>
<td><input name='h1k0' id='h1k0' type=text value='1.0' /></td>
<td><input name='h1k1' id='h1k1' type=text value='1.1' /></td>
<td><input name='h1k2' id='h1k2' type=text value='1.2' /></td>
<td><input name='h1k3' id='h1k3' type=text value='1.3' /></td>
<td><input name='h1k4' id='h1k4' type=text value='1.4' /></td>
<td><input name='h1k5' id='h1k5' type=text value='1.5' /></td>
</tr>
<tr>
<td><input name='h2k0' id='h2k0' type=text value='2.0' /></td>
<td><input name='h2k1' id='h2k1' type=text value='2.1' /></td>
<td><input name='h2k2' id='h2k2' type=text value='0.2' /></td>
<td><input name='h2k3' id='h2k3' type=text value='2.3' /></td>
<td><input name='h2k4' id='h2k4' type=text value='2.4' /></td>
<td><input name='h2k5' id='h2k5' type=text value='2.5' /></td>
</tr>
<tr>
<td><input name='h3k0' id='h3k0' type=text value='3.0' /></td>
<td><input name='h3k1' id='h3k1' type=text value='3.1' /></td>
<td><input name='h3k2' id='h3k2' type=text value='3.2' /></td>
<td><input name='h3k3' id='h3k3' type=text value='3.3' /></td>
<td><input name='h3k4' id='h3k4' type=text value='3.4' /></td>
<td><input name='h3k5' id='h3k5' type=text value='3.5' /></td>
</tr>
</table>
<script language="javascript">
function getResult(num,n){
return Math.round(num*Math.pow(10,n))/Math.pow(10,n);
}
for (JsZI = 0; JsZI < 6; JsZI++) {
var total = 0, h = 0, k = 0;
for (JsZN = 0; JsZN < 4; JsZN++) {
total += parseFloat(document.getElementById("h"+JsZN+"k"+JsZI).value);
if (document.getElementById("h"+JsZN+"k"+JsZI).value == 0.2){
h = JsZN;
k = JsZI;
}
document.getElementById("h"+h+"k"+k).value = getResult(total,2);
}
}
</script>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input name='h0k0' id='h0k0' type=text value='0.0' /></td>
<td><input name='h0k1' id='h0k1' type=text value='0.1' /></td>
<td><input name='h0k2' id='h0k2' type=text value='0.2' /></td>
<td><input name='h0k3' id='h0k3' type=text value='0.3' /></td>
<td><input name='h0k4' id='h0k4' type=text value='0.4' /></td>
<td><input name='h0k5' id='h0k5' type=text value='0.5' /></td>
</tr>
<tr>
<td><input name='h1k0' id='h1k0' type=text value='1.0' /></td>
<td><input name='h1k1' id='h1k1' type=text value='1.1' /></td>
<td><input name='h1k2' id='h1k2' type=text value='1.2' /></td>
<td><input name='h1k3' id='h1k3' type=text value='1.3' /></td>
<td><input name='h1k4' id='h1k4' type=text value='1.4' /></td>
<td><input name='h1k5' id='h1k5' type=text value='1.5' /></td>
</tr>
<tr>
<td><input name='h2k0' id='h2k0' type=text value='2.0' /></td>
<td><input name='h2k1' id='h2k1' type=text value='2.1' /></td>
<td><input name='h2k2' id='h2k2' type=text value='0.2' /></td>
<td><input name='h2k3' id='h2k3' type=text value='2.3' /></td>
<td><input name='h2k4' id='h2k4' type=text value='2.4' /></td>
<td><input name='h2k5' id='h2k5' type=text value='2.5' /></td>
</tr>
<tr>
<td><input name='h3k0' id='h3k0' type=text value='3.0' /></td>
<td><input name='h3k1' id='h3k1' type=text value='3.1' /></td>
<td><input name='h3k2' id='h3k2' type=text value='3.2' /></td>
<td><input name='h3k3' id='h3k3' type=text value='3.3' /></td>
<td><input name='h3k4' id='h3k4' type=text value='3.4' /></td>
<td><input name='h3k5' id='h3k5' type=text value='3.5' /></td>
</tr>
</table>
<script language="javascript">
function getResult(num,n){
return Math.round(num*Math.pow(10,n))/Math.pow(10,n);
}
for (JsZI = 0; JsZI < 6; JsZI++) {
var total = 0, h = [], k = [];
for (JsZN = 0; JsZN < 4; JsZN++) {
total += parseFloat(document.getElementById("h"+JsZN+"k"+JsZI).value);
if (document.getElementById("h"+JsZN+"k"+JsZI).value == 0.2){
h[h.length] = JsZN;
k[k.length] = JsZI;
}
}
for(var i=0;i<h.length;i++){
document.getElementById("h"+h[i]+"k"+k[i]).value = getResult(total,2);
}
}
</script>
</body>
</html>