项目经理写的代码,自己做了部分控制
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>欢迎页信息</title>
</head>
<body>
<table id="mytabble">
<tr>
<th>页数</th>
<th>页码范围</th>
</tr>
<tr>
<td><input class="txt1" type="text" id="numText1"/></td>
<td><input class="txt2" type="text" id="numRang1"/></td>
</tr>
<tr>
<td><input class="txt1" type="text" id="numText2"/></td>
<td><input class="txt2" type="text" id="numRang2"/></td>
</tr>
<tr>
<td><input class="txt1" type="text" id="numText3"/></td>
<td><input class="txt2" type="text" id="numRang3"/></td>
</tr>
<tr>
<td><input class="txt1" type="text" id="numText4"/></td>
<td><input class="txt2" type="text" id="numRang4"/></td>
</tr>
</table>
<script src="./jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$(".txt1").change(function(){
var numArray = new Array();
$("#mytabble tr").each(function(trindex,tritem){
$(tritem).find("td:eq(0)").each(function(tdindex,tditem){
var curVal=$(tditem).find("input").val();
if(curVal.trim().length>0){//当输入值有效才放入数组当中
numArray[trindex-1] = $(tditem).find("input").val();
}
});
});
console.log("numArray数组的长度:"+numArray.length);
console.log("numArray数组的值:"+numArray);
$("#mytabble tr").each(function(trindex,tritem){
var oneVal=$(tritem).find("td:eq(0)").find("input").val();
if(oneVal){
$(tritem).find("td:eq(1)").each(function(tdindex,tditem){
var currentNum=parseInt(numArray[trindex-1]);
if((trindex-1)==0){
$(tditem).find("input").val("1-"+currentNum);
}else{
var numSumStart=0;
$.each(numArray,function(index,value){
if(value){
numSumStart+=parseInt(value);
}
if((trindex-2)==index){
return false;
}
});
var numSumEnd=0;
$.each(numArray,function(index,value){
if(value){
numSumEnd+=parseInt(value);
}
if((trindex-1)==index){
return false;
}
});
$(tditem).find("input").val((numSumStart+1)+"-"+numSumEnd);
}
});
}
});
});
});
</script>
</body>
</html>
=====================================================注意
若表格没有标题头,则trindex-1地方改为trindex, trindex-2的地方改为trindex-1