2016-06-23 73 views
0

我有下面的PHP代碼:如何在php中使用jquery循環刪除每一行?

<tr class="delete_ex_ph_label_data">    
    <td align="left"> 
     <select name="phone_label_ex[]" class="select2 work_phone_class"> 
      <option value="">--Select--</option> 
      <?php 
      $get_orderby = mysqli_query($link, "SELECT order_type FROM contact_label_type"); 
      $get_orderby_re = mysqli_fetch_array($get_orderby); 
      $ph_orderby = $get_orderby_re['order_type']; 
      $get_label_type = mysqli_query($link, "SELECT * FROM contact_label_type WHERE clid = '1' ORDER BY order_type $ph_orderby"); 

      while($get_label_type_re = mysqli_fetch_array($get_label_type)){ 
      $label_type_id = (int) $get_label_type_re['ctid']; 
      $label_type = inputvalid($get_label_type_re['contact_label_type']); 

      if($ctid_d_cl == $label_type_id){ 
      $sel = 'selected="selected"'; 
      }else{ 
      $sel = ''; 
      } 

      echo "<option value=' $label_type_id' $sel> $label_type</option>"; 
      } 
      ?> 
     </select> 
    </td> 
    <td align="right">     
     <input type="text" name="phone_label_value_ex[]" class="small3 work_phone_class" id="work_phone" placeholder="phone"value="<?php echo $data_cl; ?>" /> 
     <input type="hidden" name="ctid" value="<?php echo $ctid_d_cl; ?>"> 
     <input type="hidden" name="label_id" value="1"> 
     <input type="hidden" name="phone_label_cdid_ex[]" value="<?php echo $cdid_d_cl; ?>"> 
     <input type="hidden" name="phone_mid[]" value="<?php echo $id_d_cl; ?>"> 
     <spam class="delete_ex_ph_label"><a href='#'>&nbsp;X</a></spam> 
    </td>    
</tr>  

這顯示波紋圖像:

enter image description here

現在,當我點擊(+)簽署呈現給各行(X)的標誌, Like波紋管圖像:

enter image description here

現在我想通過點擊(X)符號來刪除/隱藏每一行。所以,我使用下面的jQuery代碼:

$(".delete_ex_ph_label", $(this)).on('click', function() {   
    $(".delete_ex_ph_label_data", $(this)).remove(); 
}); 

但它不刪除,如何刪除通過點擊每行(X)簽署?

回答

1

$()函數中的第二個參數用於上下文(作用域)。 http://api.jquery.com/jquery/

在您的事件處理程序中,$(this)引用單擊的X,該X不包含具有「.delete_ex_ph_label_data」類的元素。相反,修改您的事件處理程序以將祖先鏈上升到第一個「.delete_ex_ph_label_data」並刪除它。

$(".delete_ex_ph_label").on('click', function() {   
    $(this).parents('.delete_ex_ph_label_data').remove(); 
}); 

Fiddle here

+0

恰到好處:) –