2011-09-01 40 views
0

我想使用jquery ajax刪除一些數據,特別是配方的成分,我使用CodeIgniter框架,但它失敗。這裏是我的代碼的部分:正在刪除的數據不能正常工作

$(".delete").live('click',function() 
{ 
var ri_id = $(this).attr('id'); 
if(confirm("Are you sure you want to delete this ingredient? Action cannot be undone.")) 
{ 
    var r_id = "<?php echo $this->uri->segment(3); ?>"; 
    alert(ri_id +" " +r_id); 
    $.ajax({ 
    type: "POST", 
    url: "/ingredient/delete", 
    data: "ri_id="+ri_id +"&r_id=" +r_id, 
    success: function(){ 
     $(this).parent().parent().parent().remove(); 
    }, 
    failure : alert('fail') 
    }); 

那麼這裏就是我爲ingredient.php:

class Ingredient extends CI_Controller { 

    function delete() 
    { 
     $data['ri_id']= $this->input->post('ri_id'); 
     $data['r_id']= $this->input->post('r_id'); 

     if (!empty($data['id'])) 
     { 
      $this->load->model('ingredient_model', '', TRUE); 
      $this->ingredient_model->delete_recipe_ingr($data); 
      $this->output->set_output('works'); 
     } 
     else 
     { 
      $this->output->set_output('dontwork'); 
     } 
    } 

} 

和我的模型:

class Recipe_model extends CI_Model 
    { 
     public function delete_recipe_ingr($data) 
     { 
      $this->db->where($data); 
      $this->db->delete('st_recipe_ingredients'); 
     } 
    } 

我錯過了什麼或者是我的代碼那是錯的?我將衷心感謝您的幫助。提前致謝。

+0

成功部分不執行,只有失敗部分。沒有控制檯錯誤。我可以看到提示框中顯示'失敗'。 – Atasha

+0

我認爲你的意思是'error:function(xhr,status,error){alert(error)}',沒有'failure'選項並且表示'失敗:alert('fail')'將執行'alert()立即。 –

+0

我明白了。感謝那。我沒有錯誤,但我仍然試圖刪除的數據無法正確執行。 :( – Atasha

回答

1

我的第一個猜測是,this是不是你認爲它是在這裏:

success: function(){ 
    $(this).parent().parent().parent().remove(); 
}, 

fine manual

context
This object will be made the context of all Ajax-related callbacks. By default, the context is an object that represents the ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax).

所以你success回調裏面,this是一個很值得簡單的對象而不是DOM中的某些東西。

有兩種標準的解決方案。你可以參考保存到this和使用:

var self = this; 
$.ajax({ 
    // ... 
    success: function() { 
     $(self).parent().parent().parent().remove(); 
    }, 
    // ... 

或使用context選項$.ajax

$.ajax({ 
    // ... 
    context: this, 
    success: function() { 
     $(this).parent().parent().parent().remove(); 
    }, 
    // ... 

,當然,作爲在評論中討論解決了failure東西。

+0

我知道了。仍然存在,就是它們只是在頁面上被刪除而不在數據庫中,我的參數是否在我的類型,網址和數據中?是否有機會檢查我傳遞給控制器​​的內容?是我在url上表示的路徑嗎?對不起,這是我第一次做這樣的事情。請耐心等待。: – Atasha

+0

@Jagad:我不確定PHP方面的事情,對不起。在你的問題上的標籤,所以希望有人會來,並幫助你解決你的問題的另一半 –

+0

我現在得到它:)謝謝,我只需要修復我的控制器和模型的腳本 – Atasha