之前的问题,大家可以参考一下http://topic.csdn.net/u/20090903/16/b2d442ad-3aa5-45dc-b21c-312718555841.html现在的问题有点复杂了,我JS不是很好,实在想不出如何解决下面的问题,想要的效果在下图中所示。
代码如下(现在能做到是找到0.00001,并将纵向所有项的和赋值给它们):<!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 align="center">5</td>
<td align="center">8</td>
<td align="center">6.5</td>
<td align="center">3</td>
<td align="center">9</td>
<td align="center">15.5</td>
</tr>
<tr>
<td><input name='h0k0' id='h0k0' type=text value='0.0' /></td>
<td><input name='h0k1' id='h0k1' type=text value='0.00001' /></td>
<td><input name='h0k2' id='h0k2' type=text value='0.2' /></td>
<td><input name='h0k3' id='h0k3' type=text value='0.00001' /></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='0.00001' /></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='0.00001' /></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='0.00001' /></td>
<td><input name='h2k2' id='h2k2' type=text value='2.2' /></td>
<td><input name='h2k3' id='h2k3' type=text value='0.00001' /></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='0.00001' /></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">
//该函数将小数保留2位
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.00001){
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>
代码如下(现在能做到是找到0.00001,并将纵向所有项的和赋值给它们):<!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 align="center">5</td>
<td align="center">8</td>
<td align="center">6.5</td>
<td align="center">3</td>
<td align="center">9</td>
<td align="center">15.5</td>
</tr>
<tr>
<td><input name='h0k0' id='h0k0' type=text value='0.0' /></td>
<td><input name='h0k1' id='h0k1' type=text value='0.00001' /></td>
<td><input name='h0k2' id='h0k2' type=text value='0.2' /></td>
<td><input name='h0k3' id='h0k3' type=text value='0.00001' /></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='0.00001' /></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='0.00001' /></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='0.00001' /></td>
<td><input name='h2k2' id='h2k2' type=text value='2.2' /></td>
<td><input name='h2k3' id='h2k3' type=text value='0.00001' /></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='0.00001' /></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">
//该函数将小数保留2位
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.00001){
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>
解决方案 »
- apply()方法的疑问
- Java程序中解析js文件提示"Request" is not defined
- window.open得到子窗体问题
- 求救,关于alert
- 大家看看这个帖子,真逗~~~
- div层慢慢变透明并最后隐藏的效果!
- 怎样实现网页导出word,excel带图片
- 十万火急,radio的onclick事件如何传递参数到其他页,在其它页如何获得传递过来的参数?
- 把web上的一个表格或者图片删除,除了用table.removeNode(false)还有其他方法吗??不能用hidden
- 可不可以定义提交的间隔时间?????
- open和showModelDialog两个方法有区别吗?
- lhgcore -> lhgeditor小型在线编辑器组件发布
<!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=utf-8" />
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td align="center">5</td>
<td align="center">8</td>
<td align="center">6.5</td>
<td align="center">3</td>
<td align="center">9</td>
<td align="center">15.5</td>
</tr>
<tr>
<td><input name='h0k0' id='h0k0' type=text value='0.0' /></td>
<td><input name='h0k1' id='h0k1' type=text value='0.00001' /></td>
<td><input name='h0k2' id='h0k2' type=text value='0.2' /></td>
<td><input name='h0k3' id='h0k3' type=text value='0.00001' /></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='0.00001' /></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='0.00001' /></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='0.00001' /></td>
<td><input name='h2k2' id='h2k2' type=text value='2.2' /></td>
<td><input name='h2k3' id='h2k3' type=text value='0.00001' /></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='0.00001' /></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">
//给在同一列的input 增加自定义属性y 这里是关键
$("tr").each(function(){
var td_num=0;
$(this).children().children().each(function(){
$(this).attr("y",td_num);
td_num++;
})
})
//点击input 查找所有input 属性y都相同的 元素
$("input").click(function(){
var y=$(this).attr("y");
var val=0;//value不是0.00001的相加
var i=0;//计算个数
$("input[y='"+y+"']").each(function(){
if($(this).val()!="0.00001"){
val=val+parseFloat($(this).val());
i++;
}
})
if(i!=0){
val=val/i;//求平均值
$("input[y='"+y+"']").each(function(){
if($(this).val()=="0.00001"){
$(this).val(val);//写入平均值
}
})
}
})</script>
</body>
</html>
只要给同一列的input增加一个属性y 同时附相同值然后在查找所有属性y相同的input 你这里是5行 当然就只有5个了在这5个里面进行分析就很明了了
var icount=0;
var frm=document.getElementById("form1").getElementsByTagName("input"); //获取窗体中所有的input元素
for(var i=0;i<frm.length;i++){ //遍历每一个input元素
if(frm[i].id=="h"+JsZN+"k"+JsZI" && frm[i].value=="0.00001"){ //判断input元素的类型
icount++; //是则给统计变量+1
}
}
document.getElementById("h"+JsZN+"k"+JsZI").value=icount;我想法是将“预设值”用数组存储起来
var arrNum=new Array("5","8","6.5","3","9","15.5")然后有个判断if(总列数-这列中'0.00001'的个数)==0{
就从arrNum数组中将该列相应的“预设值”赋值给这一列所有input
}
else if (总列数-这列中'0.00001'的个数)>0{
这一列的总数和/这列中'0.00001'的个数 赋值给这一列所有等于'0.00001'的input //为什么是总数和,因为加'0.00001'后再取小数两位,就可以忽略不计。
}
<!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 id ="tbl" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">5</td>
<td align="center">8</td>
<td align="center">6.5</td>
<td align="center">3</td>
<td align="center">9</td>
<td align="center">15.5</td>
</tr>
<tr>
<td><input name='h0k0' id='h0k0' type=text value='0.0' /></td>
<td><input name='h0k1' id='h0k1' type=text value='0.00001' /></td>
<td><input name='h0k2' id='h0k2' type=text value='0.2' /></td>
<td><input name='h0k3' id='h0k3' type=text value='0.00001' /></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='0.00001' /></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='0.00001' /></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='0.00001' /></td>
<td><input name='h2k2' id='h2k2' type=text value='2.2' /></td>
<td><input name='h2k3' id='h2k3' type=text value='0.00001' /></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='0.00001' /></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">var targetVal = 0.00001;
var cols = 6;
var rows = 4;
var defaultVal = [];
var row = document.getElementById("tbl").rows[0]
for (var i=0;i<cols ; i++){
defaultVal[i] = row.cells[i].innerText;
}
//该函数将小数保留2位
function getResult(num,n){
return Math.round(num*Math.pow(10,n))/Math.pow(10,n);
}
for (JsZI = 0; JsZI < cols; JsZI++) {
var total = 0, h = [], k = [];
for (JsZN = 0; JsZN < rows; JsZN++) {
total += parseFloat(document.getElementById("h"+JsZN+"k"+JsZI).value);
if (document.getElementById("h"+JsZN+"k"+JsZI).value == targetVal){
h[h.length] = JsZN;
k[k.length] = JsZI;
}
}
var len = h.length;
for(var i=0;i<len;i++){
var obj = document.getElementById("h"+h[i]+"k"+k[i]);
switch (len)
{
case 1:
obj.value = getResult((total-targetVal)/3,2);
break;
case 2:
obj.value = getResult((total-targetVal*2)/2,2);
break;
case 4:
obj.value = defaultVal[k[i]];
break;
}
}
}
</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><form action="" method="post" name="form1" id="form1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">5</td>
<td align="center">8</td>
<td align="center">6.5</td>
<td align="center">3</td>
<td align="center">9</td>
<td align="center">15.5</td>
</tr>
<tr>
<td><input name='h0k0' id='h0k0' type=text value='0.0' /></td>
<td><input name='h0k1' id='h0k1' type=text value='0.00001' /></td>
<td><input name='h0k2' id='h0k2' type=text value='0.2' /></td>
<td><input name='h0k3' id='h0k3' type=text value='0.00001' /></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='0.00001' /></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='0.00001' /></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='0.00001' /></td>
<td><input name='h2k2' id='h2k2' type=text value='2.2' /></td>
<td><input name='h2k3' id='h2k3' type=text value='0.00001' /></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='0.00001' /></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>
</form>
<script language="javascript">
//该函数将小数保留2位
function getResult(num,n){
return Math.round(num*Math.pow(10,n))/Math.pow(10,n);
}
var arrNum=new Array("5","8","6.5","3","9","15.5");
for (loopX = 0; loopX < 6; loopX++) {
var total = 0, countY=0, arrY = [], arrX = [];
for (loopY = 0; loopY < 4; loopY++) {
total += parseFloat(document.getElementById("h"+loopY+"k"+loopX).value);
if (document.getElementById("h"+loopY+"k"+loopX).value == 0.00001){
arrY[arrY.length] = loopY;
arrX[arrX.length] = loopX;
}
}
for(var i=0;i<arrY.length;i++){
if (4-arrY.length==0){
document.getElementById("h"+arrY[i]+"k"+arrX[i]).value = arrNum[arrY.length-1];
}
else if (4-arrY.length>0){
document.getElementById("h"+arrY[i]+"k"+arrX[i]).value = getResult(total/(4-arrY.length),2);
}
}
}
</script>
</body>
</html>