實現簡單圖形驗證碼功能
阿新 • • 發佈:2018-12-18
HTML
<img src="" class="VerifiImg" />
<input type="hidden" id="randomNum" />
JavaScript
// 隨機數 function getRanNum() { var str = ''; for(var i = 0; i < 4; i++) { var ranNum = Math.ceil(Math.random() * 9); str += ranNum.toString(); } $("#randomNum").val(str); } getRanNum(); //獲取驗證圖片 function Refresh() { var Verificode = $("#randomNum").val(); console.log(Verificode); $('.VerifiImg').attr("src",url?code=" + Verificode); } Refresh(); //點選更換驗證碼 $('.VerifiImg').click(function() { getRanNum(); Verificode = $("#randomNum").val() Refresh(); })