2016-02-18 83 views
0

這是我的代碼:功能check()工作,但功能ChangeCaptcha()沒有表現出信輸入type="randomfield"如何在沒有php的情況下添加captcha?

<body onload="ChangeCaptcha()"> 
    <input type="text" id="randomfield" disabled><br><br>        
    <input id="CaptchaEnter" size="20" ><br><br>       
    <button type="submit" onclick="check()">Log in</button> 

    <script type="text/javascript"> 
    function ChangeCaptcha() { 
     var chars= "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
     var string_lenght = 6; 
     var ChangeCaptcha = ''; 
     for (var i=0; i<string_length; i++) { 
     var rnum = Math.floor(Math.random() * chars.length); 
     ChangeCaptcha+= chars.substring(rnum, rnum+1); 
     } 
     document.getElementById('randomfield').value= ChangeCaptcha; 
    } 
    function check() { 
     if (document.getElementById('CaptchaEnter').value == document.getElementById('randomfield').value) { 
     window.open('http://www.lolila.hol.es', '_self'); 
     } else { 
     alert('Pleace re-check the captcha!') 
     } 
    } 
    </script> 
</body> 
+0

究竟什麼是您所遇到的問題?你期望得到什麼? –

+1

你爲什麼要做客戶端驗證碼驗證?閱讀此:http://stackoverflow.com/questions/3196335/how-to-create-simple-javascript-jquery-client-side-captcha – omerowitz

+1

錯誤的'var string_lenght = 6' >>'我 NewToJS

回答

1

我在Chrome開發者控制檯執行你的代碼就在這裏: enter image description here

它告訴我你的代碼中有一個錯字,因爲我不知道這個變量:string_length。你宣佈它爲string_lenght。要修復,請更改string_lenght => string_length。

結果:

function ChangeCaptcha() { 
     var chars= "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
     var string_length = 6; 
     var ChangeCaptcha = ''; 
     for (var i=0; i<string_length; i++) { 
     var rnum = Math.floor(Math.random() * chars.length); 
     ChangeCaptcha+= chars.substring(rnum, rnum+1); 
     } 
     document.getElementById('randomfield').value= ChangeCaptcha; 
    } 

結果: enter image description here

相關問題