日期:2014-05-16 浏览次数:20371 次
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <select id="sel"> <option value="usd">美金</option> <option value="yen">日元</option> <option value="rmb">人民币</option> </select> <input id="textbox" type="text" /> <div id="debug"></div> <script> var sel = document.getElementById('sel'), input = document.getElementById('textbox'); sel.onchange = function(){ input.value = ''; } input.onkeypress = function(event){ var e = event || window.event, key = e.keyCode || e.which, str = this.value + String.fromCharCode(key), reg; if(e.keyCode !== 0){ return true; } switch(sel.value){ case 'usd': reg = /^\d{0,14}$/; break; case 'yen': reg = /^\d{0,11}(?:\.\d{0,2})?$/; break; case 'rmb': reg = /^\d{0,9}(?:\.\d{0,2})?$/; break; } if(!reg.test(str)){ return false; } } input.onblur = function(){ if(/\.$/.test(this.value)){ this.value += "00"; } } </script> </body> </html>
------解决方案--------------------
3 楼正解,
<script>//这个地方最好完善下,不然在浏览器下可能会有问题 <script type="text/javascript">