if(tx1.value * 5 +tx2.value * 5 + tx3.value * 4 > 76){alert(!)}

解决方案 »

  1.   

    <script>
    function validate(){
    var count = document.getElementById('one').value * 5 + document.getElementById('two').value * 15 + document.getElementById('three' ).value * 8;
    var total = document.getElementById('total').lastChild.nodeValue;
    if(total >= count){
    total -= count;
    document.getElementById('total').lastChild.nodeValue = total;
    }
    if(total < count){
    document.getElementById('one').value = 0;
    document.getElementById('two').value = 0;
    document.getElementById('three').value = 0;
    alert("超了");
    }
    }
    </script>
    <table>
    <tr>
    <th>奖品</th>
    <th>数量</th>
    <th>积分</th>
    <th>剩余积分</th>
    </tr>
    <tr>
    <td>奖品1</td>
    <td><input type="text" id="one" value="0"  onchange="validate()"/></td>
    <td>5</td>
    <td id="total" rowspan="3" >76</td>
    </tr>
    <tr>
    <td>奖品2</td>
    <td><input type="text" id="two" value="0" onchange="validate()"/></td>
    <td>15</td>
    </tr>
    <tr>
    <td>奖品3</td>
    <td><input type="text" id="three" value="0" onchange="validate()" /></td>
    <td>8</td>
    <td></td>
    </tr>
    </table>
      

  2.   

    生成对应的table id 和input就行了<script> 
    var total=0;
    function validate()
    {
    var tb = document.getElementById("objtb");
    var input = tb.getElementsByTagName("input");
    var prize = new Array();
    var price = new Array();
    for(var i=0;i<input.length;i++ )
    {
    if(input[i].type=="text" && input[i].name=="prize")
    {
    input[i].value = input[i].value.replace(/[^\d]/g,"");
    prize.push(input[i].value==""?0:input[i].value);
    }
    else if(input[i].type=="hidden" && input[i].name=="price")
    {
    input[i].value = input[i].value.replace(/[^\d]/g,"");
    price.push(input[i].value==""?0:input[i].value);
    }
    }
    if(total == 0)
    total = document.getElementById("total").innerHTML;
    var count = 0;
    for(var i=0;i<prize.length ;i++ )
    {
    count += prize[i]*(price[i]==""?0:price[i])
    }
    if(total-count<0)
    {
    alert("积分不够");
    for(var i=0;i<input.length;i++ )
    {
    if(input[i].type=="text" && input[i].name=="prize")
    input[i].value = "";
    }
    document.getElementById("total").innerHTML=total;
    }
    else
    document.getElementById("total").innerHTML=total-count;
    }
    </script> 
    <table id="objtb"> 
    <tr> 
    <th>奖品 </th> 
    <th>数量 </th> 
    <th>积分 </th> 
    <th>剩余积分 </th> 
    </tr> 
    <tr> 
    <td>奖品1 </td> 
    <td><input type="text" name="prize" value="" onkeyup="validate()"/></td> 
    <td><input type="hidden" name="price" value="5">5</td> 
    <td rowspan="3" ><span id="total">76</span></td> 
    </tr>
    <tr> 
    <td>奖品2</td> 
    <td><input type="text" name="prize" value="" onkeyup="validate()"/></td> 
    <td><input type="hidden" name="price" value="15">15</td> 
    </tr> 
    <tr> 
    <td>奖品3 </td> 
    <td><input type="text" name="prize" value="" onkeyup="validate()"/></td> 
    <td><input type="hidden" name="price" value="8">8</td> 
    <td> </td> 
    </tr> 
    </table>