2013-09-16 41 views
0

的$(document)。在( '輸入' 輸入文本交換.....

我有下面的代碼。這代碼與人合作KEYUP檢測。但是,這怎麼做,如果任何單擊強通行證按鈕不起作用。

<form>  
    <input id="pass1" type="text" /> 
    <input id="pass2" type="text" /> 
    <a class="button" onclick="strongPass(8)">Strong Pass</a> 
</form> 

<script> 
$(document).on('input change keyup','#pass1, #pass2',function(){ 
      check_pass_strength_now(); 
}); 

function strongPass(){ 
    ..... 
    //with a loop generate Strong pass 
    //and replace #pass1 #pass2 value 
    //But after replace do not catch this $(document).on('input change keyup','#pass1, #pass2' 
    ..... 
} 
</script> 
+1

側面說明,在錯字'funtion strongPass()'。如果你調用check_pass_strength_now(),你爲什麼會發布strongPass函數? – j08691

+1

strongPass()對DOM做了哪些改變?運行前後的DOM看起來像什麼? – David

+1

什麼不正確?該按鈕是否導致事件被刪除?做一個演示,顯示確切的問題。 – epascarello

回答

0
<form id="profile"> 
    <input id="pass1" type="text" /> 
    <input id="pass2" type="text" /> <a class="button">Strong Pass</a> 

</form> 
<div id="pass-strength-result" class="alert alert-info" style="width:169px">Password line</div> 

jQuery的

// MY CODE START HERE 
var keylist="abcdefghijklmnopqrstuvwxyz123456789" 
var temp='' 

function generatepass(plength){ 
     temp='' 
for (i=0;i<plength;i++) 
temp+=keylist.charAt(Math.floor(Math.random()*keylist.length)) 
return temp 
} 

$(document).on('click','.button',function(){ 
    $('#profile #pass1,#profile #pass2').val(generatepass(8)); 
    check_pass_strength(); 
}); 


/*********/ 



        function check_pass_strength() { 

         var pass = $('#pass1').val(); 
         var pass2 = $('#pass2').val(); 
         var user = 'admin'; 
         $('#pass-strength-result').removeClass('short bad good strong'); 
         if (! pass) { 
          $('#pass-strength-result').html(pwsL10n.empty); 
          return; 
         } 

         var strength = passwordStrength(pass, user, pass2); 
         $('#pass-strength-result').removeClass('good strong danger bad'); 
         if (2 == strength) 
          $('#pass-strength-result').addClass('bad').html(pwsL10n.bad); 
         else if (3 == strength) 
          $('#pass-strength-result').addClass('good').html(pwsL10n.good); 
         else if (4 == strength) 
          $('#pass-strength-result').addClass('strong').html(pwsL10n.strong); 
         else if (5 == strength) 
          $('#pass-strength-result').addClass('danger').html(pwsL10n.mismatch); 
         else 
          $('#pass-strength-result').addClass('danger').html(pwsL10n.short); 

        } 
        /*$(document).on('input','#pass1, #pass2',function({check_pass_strength();});*/ 
        $('#pass1, #pass2').val('').keyup(check_pass_strength).change(check_pass_strength); 

      pwsL10n = { 
       empty: "Password line", 
       short: "Very easy", 
       bad: "Easy", 
       good: "Medium", 
       strong: "High", 
       mismatch: "Different Password" 
      } 
      try{convertEntities(pwsL10n);}catch(e){}; 


//------------------- Another include (Plug-in)------------------ 

// Password strength meter 
function passwordStrength(password1, username, password2) { 
    var shortPass = 1, badPass = 2, goodPass = 3, strongPass = 4, mismatch = 5, symbolSize = 0, natLog, score; 

    // password 1 != password 2 
    if ((password1 != password2) && password2.length > 0) 
     return mismatch 

    //password < 4 
    if (password1.length < 4) 
     return shortPass 

    //password1 == username 
    if (password1.toLowerCase() == username.toLowerCase()) 
     return badPass; 

    if (password1.match(/[0-9]/)) 
     symbolSize +=10; 
    if (password1.match(/[a-z]/)) 
     symbolSize +=26; 
    if (password1.match(/[A-Z]/)) 
     symbolSize +=26; 
    if (password1.match(/[^a-zA-Z0-9]/)) 
     symbolSize +=31; 

    natLog = Math.log(Math.pow(symbolSize, password1.length)); 
    score = natLog/Math.LN2; 

    if (score < 40) 
     return badPass 

    if (score < 56) 
     return goodPass 

    return strongPass; 
} 

演示:http://jsfiddle.net/iOnur/rqDdp/

-1

嘗試在你的strongPass函數的末尾添加這個(退出循環)

function strongPass(){ 
    ..... 
    //with a loop generate Strong pass 
    //and replace #pass1 #pass2 value 
    //But after replace do not catch this $(document).on('input change keyup','#pass1, #pass2' 
    ..... 

    $("#pass1").trigger('keyup'); 
    $("#pass2").trigger('keyup'); 
}