<form name="tradeSilverExchangeForm" method="post" action="/Project/tradeSilverExchange.do">
<input type="text" name="crystal" value="0" onKeyUp="zycs();">
<input type="text" name="matal" value="0" onKeyUp="zycs();">
<input type="text" name="foodStuff" value="0" onKeyUp="zycs();">
<input type="submit" value="确认卖出">
<script>var maxsj=1000;var maxjs=1000;var maxls=1000;</script>
<script>
function zycs(){
if(parseInt(document.tradeSilverExchangeForm.crystal.value)>maxsj){document.tradeSilverExchangeForm.crystal.value=maxsj;charAt(maxsj);}
if(parseInt(document.tradeSilverExchangeForm.matal.value)>maxjs){document.tradeSilverExchangeForm.matal.value=maxsj;charAt(maxjs);}
if(parseInt(document.tradeSilverExchangeForm.foodStuff.value)>maxls){document.tradeSilverExchangeForm.foodStuff.value=maxsj;charAt(maxls);}
}
</script>
我写了这么一个脚本,我现在有这种需求就是输入框里面只能输入正整数,我想在此函数的基础上改良一下,不知道怎么改。原来我是在输入框里面加入了onkeyup="this.value=this.value.replace(/[^\d]/g,'') " onafterpaste="this.value=this.value.replace(/[^\d]/g,'') " 这个使得他只能输入正整数,但是我如果在加上onKeyUp="zycs();"这个的话,前面的就不起作用了,怎么把这个两个条件结合起来写在一个函数里面。 

解决方案 »

  1.   

    <form name="tradeSilverExchangeForm" method="post" action="/Project/tradeSilverExchange.do"> 
    <input type="text" name="crystal" value="0" onKeyUp="zycs(this);" onafterpaste="zycs(this);"> 
    <input type="text" name="matal" value="0" onKeyUp="zycs(this);" onafterpaste="zycs(this);"> 
    <input type="text" name="foodStuff" value="0" onKeyUp="zycs(this);" onafterpaste="zycs(this);"> 
    <input type="submit" value="确认卖出"> 
    <script>var maxsj=1000;var maxjs=1000;var maxls=1000; </script> 
    <script> 
    function zycs(o){ 
    o.value=o.value.replace(/[^\d]/g,'');
    if(parseInt(document.tradeSilverExchangeForm.crystal.value)>maxsj) {document.tradeSilverExchangeForm.crystal.value=maxsj;charAt(maxsj);} 
    if(parseInt(document.tradeSilverExchangeForm.matal.value)>maxjs) {document.tradeSilverExchangeForm.matal.value=maxsj;charAt(maxjs);} 
    if(parseInt(document.tradeSilverExchangeForm.foodStuff.value)>maxls) {document.tradeSilverExchangeForm.foodStuff.value=maxsj;charAt(maxls);} 

    </script>