2015-09-27 59 views
0

我有HTML代碼,基於jQuery圖像加載或沒有加載這樣如何在jQuery的

... 
<title>Untitled Document</title> 
</head> 
<script type="text/javascript" src="bootstrap/js/jquery.js"></script> 
      <script type="text/javascript"> 
$(document).ready(function(){ 

$("#image11") 
.load(function(){ 
    alert('oke1'); 
    $("#upload1").hide(); 
    $("#image21").hide(); 

}) 
.error(function(){ 
    alert('oke2'); 
    $("#image11").hide(); 
    $("#upload1").hide(); 
    $("#image21") 
     .load(function(){ 
      alert('oke3'); 
      $("#upload1").hide(); 

      $("#image11").hide(); 
    }) 
    .error(function(){ 
     alert('oke4'); 
      $("#upload1").show(); 
      $("#image21").hide() 
      $("#image11").hide(); 
     }); 
    }); 

}) 

      </script> 
<body> 
    <div class="col-sm-5 col-xs-7"> 
    <img src="foto_peserta/tes.jpg" width="100" id="image11" alt="tes"/> 
    <img src="foto_peserta/cek.jpg" width="100" id="image21" alt="tes2"/>   
    </div> 
<div id="upload1"> 
<form method="post" name="form1" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data"><img id="previewHolder" alt="Foto" width="100px" height="100px"/> <p> 
    <input type="file" name="foto_peserta" id="foto_peserta" required> 
    <p class="help-block">maximum image size is 50 kB,only JPG, JPEG, PNG &amp; GIF files are allowed</p> 
    <button type="submit" class="btn btn-success">Upload Foto</button> 
       <input type="hidden" name="MM_update" value="form1" > 
       <input type="hidden" name="id_personal" value="<?php echo $row_foto_peserta_tampil['id_personal']; ?> "> 
    </form></div> 


</body> 
</html> 

我只是困惑,爲什麼我每次刷新頁面,以顯示和隱藏窗體加載功能的行爲(當我image21是負載,我的image11未加載)警報('oke2')提示,但不與alert('oke3'),是我的代碼有什麼問題嗎?

回答

1

你所談論的,因爲jQuery的1.8已被棄用負載的功能:

在以後的版本中只一個​​函數留在jQuery中,爲了避免模糊性,它被用來從另一個數據源加載數據例如,如果您需要將來自服務器的文本加載到頁面中的div中。

您可以檢查圖像通過使用

$('img.mustLoad').on('load',function(){ 
     /* Fire your image resize code here */ 
}); 

這個非常複雜的片斷是從我上面貼的問題的目前第二個答案了這麼信貸是由於「亞歷克斯W」加載。這應該與jQuery

的每一個版本的工作,以處理加載錯誤,你需要使用一個單獨的事件

$('img.mustLoad').on('error',function(){ 
     /* Fire your image resize code here */ 
}); 

所以,你需要編輯代碼相應

+0

感謝您的回覆,這個解決我的問題。 – eniac05