2016-01-16 54 views
2

我有一個包含密碼錶的登錄表單的項目。我跟着a tutorial from cssdeck,然後試着將代碼複製粘貼到JSFiddle以查看結果。它適用於演示版,但不適用於my JSFiddle。問題是密碼錶不起作用。我已經閱讀了JavaScript,看不出有什麼問題。
任何建議都會很好。爲什麼完全相同的代碼不能在小提琴中使用?Javascript未運行或正在運行?

$(function(){ 
    var pass1 = $('#password1'), 
     pass2 = $('#password2'), 
     email = $('#email'), 
     form = $('#main form'), 
     arrow = $('#main .arrow'); 

    // Empty the fields on load 
    $('#main .row input').val(''); 

    // Handle form submissions 
    form.on('submit', function(e){ 
     // Is everything entered correctly? 
     if ($('#main .row.success').length == $('#main .row').length) { 
      // Yes! 
      alert("Thank you for trying out this demo!"); 
      e.preventDefault(); 
      // Remove this to allow actual submission 
     } else { 
      // No. Prevent form submission 
      e.preventDefault(); 
     } 
    }); 

    // Validate the email field 
    email.on('blur', function(){ 
     // Very simple validation 
     if (!/^\[email protected]\S+\.\S+$/.test(email.val())) { 
      email.parent().addClass('error').removeClass('success'); 
     } else { 
      email.parent().removeClass('error').addClass('success'); 
     } 
    }); 

    // Use the complexify plugin on the first password field 
    pass1.complexify({minimumChars: 6, strengthScaleFactor: 0.7}, 
     function(valid, complexity){ 
      if (valid) { 
       pass2.removeAttr('disabled'); 
       pass1.parent().removeClass('error').addClass('success'); 
      } else { 
       pass2.attr('disabled','true'); 
       pass1.parent().removeClass('success').addClass('error'); 
      } 

      var calculated = (complexity/100) * 268 - 134; 
      var prop = 'rotate(' + (calculated) + 'deg)'; 
      // Rotate the arrow 
      arrow.css({ 
       '-moz-transform': prop, 
       '-webkit-transform': prop, 
       '-o-transform': prop, 
       '-ms-transform': prop, 
       'transform': prop 
      }); 
     } 
    ); 

    // Validate the second password field 
    pass2.on('keydown input', function(){ 
     // Make sure its value equals the first's 
     if (pass2.val() == pass1.val()) { 
      pass2.parent().removeClass('error').addClass('success'); 
     } else { 
      pass2.parent().removeClass('success').addClass('error'); 
     } 
    }); 
}); 

還包括jquery.complexify插件。

+0

密碼錶適用於我的小提琴。你看到控制檯中有任何錯誤嗎? – TheDude

+1

您是否在頁面中正確包含了[jQuery](https://jquery.com/download/)? –

+0

它似乎在原始文章中適用於我,但不適用於我的項目。這裏是一個小提琴 - https://jsfiddle.net/9hkdL51p/ –

回答

5

您的代碼中沒有包含jQuery庫。

這是從你的提琴 enter image description here

你需要在你的頁面添加jQuery的https://jsfiddle.net/5n90vLgn/

後,包括jQuery的像這樣的小提琴 enter image description here

這是你的相同的代碼提琴你需要包括它之前寫你的js代碼或包括一個外部的js文件,包括jqu從CDN (*) ERY,即

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script> 

,或者包括像jQuery的本地副本:

<script src="path/to/jquery.js/"></script> 

(*)以上CDN是從jQuery的網站上,有是jquery的其他CDN,雖然像Google librariesCDNjs libraries

+0

謝謝!求助 –

+0

您好,我很高興它幫助,享受編碼 –

+1

@HoneyBadgerYTB我不是回答的人,但萬一你忘了,接受(點擊這個答案左邊的綠色勾號),答案會有幫助。 – maowtm