2014-08-28 30 views
0

我很困惑我的append()沒有顯示在HTML中。你們能告訴我什麼是錯在下面的代碼:jQuery prepend()不工作,出了什麼問題

<div class="form-group" id="password"> 
    <label class="control-label" for="inputError"> Password:</label> 
    <input type="password" class="form-control" id="input-password" placeholder="Password" value="" name="password"/> 
</div> 

我的JS:

<script type="text/javascript"> 
     $('input[name="password"]').focusout(function() { 
      if($(this).val()==""){ 
       $('#password').attr('class','form-group has-error'); 
       $('#password .control-label').prepend("<span class='fa fa-times-circle-o'></span>"); 
       $('#password .control-label').text(' Input your new password!'); 
      } 
     }); 
</script> 
+2

將jQuery的文件準備功能 – kamesh 2014-08-28 11:30:45

回答

3

假設事件的內容事件被觸發,問題是你.prepend後使用的.text() (),這將除去()與.prepend添加的內容

$('input[name="password"]').focusout(function() { 
    if ($(this).val() == "") { 
     $('#password').attr('class', 'form-group has-error'); 
     $('#password .control-label').text(' Input your new password!'); 
     $('#password .control-label').prepend("<span class='fa fa-times-circle-o'></span>"); 
    } 
}); 

演示:Fiddle


所以

jQuery(function($){ 
    $('input[name="password"]').focusout(function() { 
     if ($(this).val() == "") { 
      $('#password').attr('class', 'form-group has-error'); 
      $('#password .control-label').text(' Input your new password!').prepend("<span class='fa fa-times-circle-o'></span>"); 
      //$('#password .control-label').html('<span class="fa fa-times-circle-o"></span> Input your new password!'); 
     } 
    }); 
}); 
+0

內jQuery代碼,你可以IT連鎖http://jsfiddle.net/rft6p7tc/已經更新 – andrew 2014-08-28 11:37:14

+0

@andrew,謝謝!我不知道這導致了這個問題,我花了一個小時解決了這個錯誤。 – 2014-08-28 11:37:50

+0

哦 – 2014-08-28 13:36:03