2016-03-08 46 views
2

//顯示採取我想要得到每個的值​​<button></td>。這裏是我的代碼到目前爲止

//我想要得到它們各自的價值所有訂單選擇

$.ajax({ 
    url: 'http://www.api.verdulero.com/index.php/api/Pedidos/mostrar_fila? id_pedido='+id_pedi, 
    type:'get',       
    success: function(response){  

    $("#tableOrder").show(); 

//顯示每個記錄在表格

$.each(response, function (index,producto){ 
    $("#displayOrder").html(''); 


    ap += "<tr>"; 
    ap += "<td>"+producto.cantidad+"</td>"; 
    ap += "<td>"+producto.unidad+"</td>"; 
    ap += "<td>"+producto.producto_nombre+"</td>"; 
    ap += "<td>"+producto.fecha_entrega+"</td>"; 
    ap += "<td></td>"; 
    ap += "<td><button type='button' id='borrar' class='btn btn-danger' value='"+producto.id+"'>Borrar</button></td>"; 
    ap += "</tr>"; 


    }); 

    $("#displayOrder").append(ap); // display the orde 

r

//刪除訂單的功能。

$("#displayOrder button").each(function(){ 
     var i = $(this).val(); 
     $("#displayOrder").on("click","button", function(){ 
      alert(i); 
      return false; 
     }); 
    }); 
} 

//最終成功顯示順序的

+0

我想刪除的每個命令,如果客戶點擊,但我不能用它獲取每個按鈕自身的價值 – kikemnzz

+0

你能更清楚一點與實際問題? – starf

回答

1

無需遍歷所有按鈕只是事件偵聽器。你可以註冊一次,並且他們都被計算在內。請參見下面的代碼:

// event delegation used 
// target all the button inside #displayOrder 
$("#displayOrder").on("click","button", function(){ 
    // this is how you captured button's value 
    // which in your case is a product ID 
    var btnValue = $(this).val(); 
    // and this to remove button's row(clicked button row) 
    $(this).closest('tr').remove(); 
}); 
+0

非常感謝Norlihazmey,這是我需要的。 – kikemnzz

+0

很高興聽到這個,歡迎你! –

0

嘗試:

$("#displayOrder").click('button', function(){ 
    $(this).parent().parent().remove(); 
}); 
相關問題