解决此js问题100分只给你一个人~
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="jquery-1.4.3.min.js"><\/script>');</script>
</HEAD>
<BODY>
<li>1.用户输入的值必须小于后面红色部分</li>
<li>2.如果CO要从20变成100,那么是从CO的下级或下级的下级里扣除,比如当CO变成100 则首先从SH扣除15 然后从SA1扣除10,SA2扣除15,SA3扣除20,SA4扣除20</li>
<li>3.如果CO从20变成0,那么CO的下级会从15变成35,就是说除了最后一级SA4,其余的如果上级增加就从下级扣除直到满足条件,如果上级减少则减少的部分会加给下级</li>
<li>4.如果把最后一级减少,那么减少的部分会添加到他的上一级,比如把SA4变成0那么SA4的上级SA3则是40</li>
<li>5.如果把CO SH SA1 都设为0 那么SA2会变成60 这时需要提示出错 并且把CO SH SA1 SA2还原成初始化的状态</li>
<br/><br/>
<tr align="left" height="15" valign="middle">
<th width="10%">
<span style="color: red;">*</span>实际风险(%)</th>
<td colspan="6" class="table01" bgcolor="#ffffff">
<div id="etd2">
CO<input size="5" onblur="check(0)" name="actualRiskVal" id="actualRiskVal0" oldvalue="20" value="20" type="text">
<span style="color: red;">/</span><span id="maxRisk0" style="color: red;">100</span><span style="color: red;">%</span>
SH<input size="5" onblur="check(1)" name="actualRiskVal" id="actualRiskVal1" oldvalue="15" value="15" type="text">
<span style="color: red;">/</span><span id="maxRisk1" style="color: red;">80</span><span style="color: red;">%</span>
SA1<input size="5" onblur="check(2)" name="actualRiskVal" id="actualRiskVal2" oldvalue="10" value="10" type="text">
<span style="color: red;">/</span><span id="maxRisk2" style="color: red;">65</span><span style="color: red;">%</span>
SA2<input size="5" onblur="check(3)" name="actualRiskVal" id="actualRiskVal3" oldvalue="15" value="15" type="text">
<span style="color: red;">/</span><span id="maxRisk3" style="color: red;">55</span><span style="color: red;">%</span>
SA3<input size="5" onblur="check(4)" name="actualRiskVal" id="actualRiskVal4" oldvalue="20" value="20" type="text">
<span style="color: red;">/</span><span id="maxRisk4" style="color: red;">40</span><span style="color: red;">%</span> <br>
SA4<input size="5" onblur="check(5)" name="actualRiskVal" id="actualRiskVal5" oldvalue="20" value="20" type="text">
<span style="color: red;">/</span><span id="maxRisk5" style="color: red;">20</span><span style="color: red;">%</span> </div>
<div id="etd"></div>
</td>
</tr>
</BODY>
<script type="text/javascript">
function check(id){
var oldVal = $("#actualRiskVal"+id).attr("oldValue");
var risk = $("#actualRiskVal"+id).val();
//console.log(oldVal);
var reg=/^[0-9]+$/;
if(!reg.test(risk))
{
alert("请输入数字");
$("#actualRiskVal"+id).val(oldVal);
}else{
var maxRisk = $("#maxRisk"+id).html();
//console.log("maxRisk"+maxRisk);
if(parseInt(risk)>parseInt(maxRisk)){
alert("必须小于最大值"+maxRisk);
$("#actualRiskVal"+id).val(oldVal);
}
}
}
</script>
</HTML>
[解决办法]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="jquery-1.4.3.min.js"><\/script>');</script>
</HEAD>
<BODY>
<li>1.用户输入的值必须小于后面红色部分</li>
<li>2.如果CO要从20变成100,那么是从CO的下级或下级的下级里扣除,比如当CO变成100 则首先从SH扣除15 然后从SA1扣除10,SA2扣除15,SA3扣除20,SA4扣除20</li>
<li>3.如果CO从20变成0,那么CO的下级会从15变成35,就是说除了最后一级SA4,其余的如果上级增加就从下级扣除直到满足条件,如果上级减少则减少的部分会加给下级</li>
<li>4.如果把最后一级减少,那么减少的部分会添加到他的上一级,比如把SA4变成0那么SA4的上级SA3则是40</li>
<li>5.如果把CO SH SA1 都设为0 那么SA2会变成60 这时需要提示出错 并且把CO SH SA1 SA2还原成初始化的状态</li>
<br/><br/>
<tr align="left" height="15" valign="middle">
<th width="10%">
<span style="color: red;">*</span>实际风险(%)</th>
<td colspan="6" class="table01" bgcolor="#ffffff">
<div id="etd2">
CO<input size="5" onblur="check(0)" name="actualRiskVal" id="actualRiskVal0" oldvalue="20" value="20" type="text">
<span style="color: red;">/</span><span id="maxRisk0" style="color: red;">100</span><span style="color: red;">%</span>
SH<input size="5" onblur="check(1)" name="actualRiskVal" id="actualRiskVal1" oldvalue="15" value="15" type="text">
<span style="color: red;">/</span><span id="maxRisk1" style="color: red;">80</span><span style="color: red;">%</span>
SA1<input size="5" onblur="check(2)" name="actualRiskVal" id="actualRiskVal2" oldvalue="10" value="10" type="text">
<span style="color: red;">/</span><span id="maxRisk2" style="color: red;">65</span><span style="color: red;">%</span>
SA2<input size="5" onblur="check(3)" name="actualRiskVal" id="actualRiskVal3" oldvalue="15" value="15" type="text">
<span style="color: red;">/</span><span id="maxRisk3" style="color: red;">55</span><span style="color: red;">%</span>
SA3<input size="5" onblur="check(4)" name="actualRiskVal" id="actualRiskVal4" oldvalue="20" value="20" type="text">
<span style="color: red;">/</span><span id="maxRisk4" style="color: red;">40</span><span style="color: red;">%</span> <br>
SA4<input size="5" onblur="check(5)" name="actualRiskVal" id="actualRiskVal5" oldvalue="20" value="20" type="text">
<span style="color: red;">/</span><span id="maxRisk5" style="color: red;">20</span><span style="color: red;">%</span> </div>
<div id="etd"></div>
</td>
</tr>
</BODY>
<script type="text/javascript">
function check(id){
var myData = data[id];
myData.setValue(myData.getValue());
}
var data = [];
for (var i = 0; i <= 5; i++) {
data[i] = {
id: i,
field: $("#actualRiskVal" + i),
limit: parseInt($("#maxRisk" + i).html()),
getValue: function() {
return this.field.val();
},
setValue: function(v) {
var reg=/^[0-9]+$/;
if(!reg.test(v)) {
alert("请输入数字");
this.field.val(this.oldValue);
return;
}
if (v > this.limit) {
alert("必须小于最大值"+this.limit);
this.field.val(this.oldValue);
return;
}
this.field.val(v);
diff = v - this.oldValue;
this.oldValue = v;
if (this.next) {
this.next.adjust(-diff);
}
},
oldValue: $("#actualRiskVal" + i).val(),
adjust: function(diff) {
if (diff == 0) {
return;
}
var myVal = parseInt(this.getValue()) + diff;
if (myVal < 0
[解决办法]
myVal > this.limit) {
myVal = Math.max(0, Math.min(myVal, this.limit));
}
var myDiff = myVal - parseInt(this.oldValue) - diff;
this.field.val(myVal);
this.oldValue = myVal;
if (this.next) {
this.next.adjust(-myDiff);
}
}
};
if (i > 0) {
data[i - 1].next = data[i];
data[i].prev = data[i - 1];
}
}
</script>
</HTML>
function check(id){
var myData = data[id];
myData.setValue(myData.getValue());
}
var data = [];
for (var i = 0; i <= 5; i++) {
data[i] = {
id: i,
field: $("#actualRiskVal" + i),
limit: parseInt($("#maxRisk" + i).html()),
originalValue: $("#actualRiskVal" + i).attr("oldvalue"), //添加了一个原始值, 用于还原成初始化的状态的时候使用
oldValue: $("#actualRiskVal" + i).val(),//记录前一个值
getValue: function() {
return this.field.val();
},
setValue: function(v) {
var reg=/^[0-9]+$/;
if(!reg.test(v)) {
alert("请输入数字");
this.field.val(this.oldValue);
return;
}
if (v > this.limit) {
alert("必须小于最大值"+this.limit);
this.field.val(this.oldValue);
return;
}
this.field.val(v);
diff = v - this.oldValue;
this.oldValue = v;
if (this.next) {
this.next.adjust(-diff);
}
},
adjust: function(diff) {
if (diff == 0) {
return;
}
var myVal = parseInt(this.getValue()) + diff;
if (myVal < 0) {
myVal = Math.max(0, Math.min(myVal, this.limit));
}
if (myVal > this.limit) {
alert("出了问题!");
for (var i = 0; i <= 5; i++) { //用于满足第5条的条件
data[i].field.val(data[i].originalValue);
data[i].oldValue = data[i].originalValue;
}
return;
}
var myDiff = myVal - parseInt(this.oldValue) - diff;
this.field.val(myVal);
this.oldValue = myVal;
if (this.next) {
this.next.adjust(-myDiff);
}
}
};
if (i > 0) {
data[i - 1].next = data[i];
data[i].prev = data[i - 1];
}
}
</script>
</HTML>