1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 3 <html xmlns="http://www.w3.org/1999/xhtml" > 4 <head> 5 <title>单据冲账金额分配</title> 6 7 <style type="text/css"> 8 tr{line-height:50px;} 9 td{background-Color:#EFEEDF;} 10 .lab_border{border:1px solid red;} 11 .lab_color{color:red;} 12 .lab_color2{color:red;} 13 </style> 14 <script src="http://www.cnblogs.com/scripts/jquery.js" type="text/javascript" ></script> 15 <script type="text/javascript"> 16 //初始化为0 17 $(function (){ 18 $("#RepayNum").val("0"); 19 show(); 20 }); 21 22 23 function show() 24 { 25 var ErrMsg=""; 26 var fee = $("#Fee").text(); 27 var RepayNum = $("#RepayNum").val(); 28 29 //判断是否为数字 30 if (isNaN(RepayNum)){ 31 ErrMsg = "非法金额!"; 32 }else{ 33 //输入数字不能大于2位小数 34 if(!RepayNum.match(/^\d*\.?\d{0,2}$/)){ 35 $("#RepayNum").val(changeTwoDecimal(RepayNum)); 36 RepayNum = $("#RepayNum").val(); 37 } 38 //判断数字范围 39 if(RepayNum>Number(fee)) 40 ErrMsg = "冲账金额不能大于本单据总金额"; 41 else 42 ErrMsg = ""; 43 } 44 45 //根据是否有错误选择样式 46 if(ErrMsg!=""){ 47 $("#PayNum").addClass("lab_color").text("Error"); 48 $("#RepayNum").addClass("lab_border"); 49 $("#RepayNum").focus(); 50 }else{ 51 $("#RepayNum").removeClass("lab_border"); 52 $("#PayNum").removeClass("lab_color").text(changeTwoDecimal(fee-RepayNum)); 53 } 54 $("#showE").text(ErrMsg) 55 } 56 //传入一多位小数,四舍五入之后保留两位小数 57 function changeTwoDecimal(x) 58 { 59 var f_x = parseFloat(x); 60 if (isNaN(f_x)) 61 { 62 alert('function:changeTwoDecimal->parameter error'); 63 return false; 64 } 65 f_x = Math.round(f_x *100)/100; 66 67 return f_x; 68 } 69 </script> 70 </head> 71 <body> 72 <form id="form1"> 73 <table width="100%"> 74 <tr> 75 <td style="text-align:right;">单据总金额:</td> 76 <td><label id="Fee">123456</label></td> 77 <td colspan="2"><label id="showE" style="color:Red"></label></td> 78 </tr> 79 <tr> 80 <td style="text-align:right; 25%">冲账金额:</td> 81 <td style=" 25%"><input type="text" id="RepayNum" style="65px; " value="0" maxlength="10" onkeyup="show()"/></td> 82 <td style="text-align:right; 25%">付款金额:</td> 83 <td style=" 25%"><label id="PayNum">123456</label></td> 84 </tr> 85 </table> 86 </form> 87 </body> 88 </html>