2014-02-13 33 views
1

這是工作,並由於某種原因不會在ajax輸出工作。jquery點擊不與阿賈克斯輸出工作

原始內容

<div id="selected_standards" class="touchcarousel black-and-white">  
    <ul id="standards_list" class="touchcarousel-container"> 
    <?php 
     $sql = "SELECT * FROM standards 
       LEFT JOIN image 
       ON image.imageID = standards.imageID_standard 
       WHERE auditID_standard = '$audit_id'"; 
     $result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli)); 

     while($row = mysqli_fetch_assoc($result)){ 
     $standardImagePath = $row['imageLocation']; 
      list($width, $height) = getimagesize($standardImagePath); 
      //landscape 
      if($width > $height){ 
       $img_width = '200'; 
       $margin = '170px'; 
      //portrait 
      }else if ($width < $height){ 
       $img_width = '120'; 
       $margin = '90px'; 
      } 
    ?>    
      <li class="touchcarousel-item"> 
       <input type="hidden" class="image_id" value="<? echo $row['imageID']; ?>"> 
       <input type="hidden" class="audit_id" value="<? echo $audit_id; ?>"> 
       <input type="button" class="del_standard_cross" name="del_standard" value="" style="margin-left:<? echo $margin; ?> !important;"> 
       <img src="<? echo $row['imageLocation']; ?>" width="<? echo $img_width; ?>" > 
      </li>    
    <?php } ?> 
    </ul> 
</div> 

phpajax響應

$output .='<ul id="standards_list" class="touchcarousel-container">'; 
    $path = "upload/".$id."/standards/"; 
    while($row = mysqli_fetch_assoc($result)){ 
     $standardImagePath = '../'.$row['imageLocation']; 
     list($width, $height) = getimagesize($standardImagePath); 
     //landscape 
     if($width > $height){ 
     $img_width = '200'; 
     $margin = '170px'; 
     //portrait 
    }else if ($width < $height){ 
     $img_width = '120'; 
     $margin = '90px'; 
    }   
    $output .='<li class="touchcarousel-item">'; 
    $output .='<input type="hidden" class="image_id" value="'.$row['imageID'].'">'; 
    $output .='<input type="hidden" class="audit_id" value="'.$audit_id.'">'; 
    $output .='<input type="button" class="del_standard_cross" 
     name="del_standard" value="" style="margin-left:'.$margin.' !important;">'; 
    $output .='<img src="'.$row['imageLocation'].'" width="'.$img_width.'" >'; 
    $output .='</li>';    
     } 
$output .='</ul>'; 

jquery不上ajax輸出

$(document).ready(function() { 
    $('.touchcarousel-item').on('click', '.del_standard_cross', function(e){ 
     alert('smeg'); 
    }); 
}); 
+0

我看到了,但不明白它是如何與我的代碼,但由於答案的交代我現在知道你爲什麼 – tatty27

回答

2

$('.touchcarousel-item')也被從加載工作一個jax請求,所以它在DOM中不存在於DOM中,因此處理程序不能附加到它。

$(document).ready(function() { 
    $('.touchcarousel-container').on('click', '.del_standard_cross', function(e){ 
     alert('smeg'); 
    }); 
}); 

Event Delegation

+1

你的超級巨星,只要它允許我接受 – tatty27

+1

請支持社區審查通過投票重複而不是回答。 Dystroy已經確定了一個副本。 –

0
$(document).ready(function() { 
    $('.touchcarousel-container').on('click', '.del_standard_cross', function(e){ 
     alert('smeg'); 
    }); 
}); 

或者也可以使用實況()事件的jQuery < 1.9