效果圖如下:
頁面代碼:
<div class="form-item form-width-in fr"> <label>產 品 編 碼</label> <input type="text" id="product_code" name="product_code" value="" class="field fwidth" placeholder="獲取隨機編碼"> <a onclick="hq();" class="mailbox" style="cursor:pointer;">獲取</a> </div>
js代碼:
<script type="text/javascript"> function hq(){ //聲明一個隨機數變量,默認為1 var GetRandomn = 1; //js生成時間戳 var timestamp=new Date().getTime(); //獲取隨機范圍內數值的函數 function GetRandom(n){ //由隨機數+時間戳+1組成 GetRandomn=Math.floor(Math.random()*n+timestamp+1); } //開始調用,獲得一個1-100的隨機數 GetRandom("30"); //把隨機數GetRandomn 賦給 input文本框.根據input的id document.getElementById('product_code').value = GetRandomn; //調試輸出查看 //alert(GetRandomn); } </script>