2014-07-15 60 views
0

我有一個簡單的腳本,提示用戶確認刪除,然後它只是提醒他們該過程已正確完成。jConfirm未定義

我嘗試使用jConfirm和JavaScript控制檯吐出: 「的ReferenceError:未定義jConfirm」

<!-- jConfirm --> 
// the script is referenced properly  
<script src="/js/jconfirm/jquery.jconfirm-1.0.min.js"></script> 


<script> 
function careersDelete(career_id) { 
    jConfirm('Are you sure you want to delete this career?', 'Delete Career', function (x) { 
    if (x) { 
     jQuery.ajax({ 
      url: "ajax_career_delete.php", 
      type: "GET", 
      data: "career_id=" + career_id, 
      success: function (response) { 
       alert('Career deleted'); 
      } 
     }); 
    } 
}); 

}

在應該使確認對話框中的按鈕,我有:

<a href="#" onclick="careersDelete('<?php echo $career_id; ?>')">delete</a> 
+0

是'jQuery'和'jConfirm'庫正確包括? – pes502

+0

是的,他們都正確加載... – Hugo

回答

2

如果你退房these examples你會發現你需要單獨提供jquery,並且jconfirm僅在其名稱空間內定義。因此,你需要在你的代碼添加了兩兩件事:

<script src="/js/jquery.js"></script> <!-- or wherever jquery.js's path is --> 
... 
    $.jconfirm('Are you sure ... 
    // note jquery's $. namespace; and the non-capitalized "c" in jconfirm 

此外,$.jconfirm函數調用沒想到字符串。它的簽名是:

function(options, callback) 

而且callback不與任何參數(因此你xundefined)執行。它看起來像你想要的東西沿線︰

function careersDelete(career_id) { 
    jQuery.jconfirm({ 
     message: 'Are you sure you want to delete this career?', 
     title: 'Delete Career' 
    }, function() { 
     jQuery.ajax({ 
      url: "ajax_career_delete.php", 
      type: "GET", 
      data: "career_id=" + career_id, 
      success: function (response) { 
       alert('Career deleted'); 
      } 
     }) 
    }); 
} 
+0

嗨blgt謝謝你幫助它說TypeError:$ .jconfirm不是一個函數。 – Hugo

+0

+1 :)很好回答 – pes502

+0

真是太棒了!非常感謝並解釋它,而不僅僅是給出答案。我必須回到帶有JS的繪圖板上,因爲有些概念我不認爲我完全理解。再次感謝。 – Hugo