2016-09-25 46 views
-1

我有一個函數(模態確認)在外部JS文件中定義它返回一個值作爲這樣的:

function confirmation(question) { 
    var defer = $.Deferred(); 
    $('<div></div>').html(question).dialog({ 
     autoOpen: true, 
     modal: true, 
     title: 'Confirmation', 
     buttons: { 
     "Delete All Items": function() { 
      defer.resolve("true"); //this text 'true' can be anything. But for this usage, it should be true or false. 
      $(this).dialog("close"); 
     }, 
     "Cancel": function() { 
      defer.resolve("false"); //this text 'false' can be anything. But for this usage, it should be true or false. 
      $(this).dialog("close"); 
     } 
     }, 
     close: function() { 
     //$(this).remove(); 
     $(this).dialog('destroy').remove() 
     } 
    }); 
} 

現在,當我試圖調用$(document).ready(function() {裏面的功能,我得到一個未捕獲引用錯誤

所有必需的文件有蜜蜂。包括在調用腳本中。我想明白爲什麼是這個以及我如何解決這個問題?

+1

參考由它自己的錯誤並沒有任何意義。你可以打開開發工具並檢查錯誤發生的位置。 –

+0

這個函數不會返回任何東西,它本身會創建一個documentfragment然後拋棄它,絕不會將該元素添加到dom中。 –

+0

在屬性事件[未捕獲的ReferenceError - 函數未定義]中調用的函數(http://stackoverflow.com/questions/39668892/uncaught-referenceerror-function-is-not-defined)? 'defer'是從函數返回的嗎? – guest271314

回答

0

除了最後缺少的花括號,並假設您的「必要文件」包含jquery-ui,您的函數似乎沒有任何問題。看到這個jsfiddle,它不會產生任何錯誤。

也許問題是在你的代碼中的其他地方?如果您可以發佈Minimal, Complete, and Verifiable example,這可能會有所幫助。

參考文獻:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.theme.css" /> 

腳本:

$(document).ready(function() { 
    confirmation("What's all this, then?"); 
}); 

function confirmation(question) { 
    var defer = $.Deferred(); 
    $('<div></div>').html(question).dialog({ 
     autoOpen: true, 
     modal: true, 
     title: 'Confirmation', 
     buttons: { 
     "Delete All Items": function() { 
      defer.resolve("true"); //this text 'true' can be anything. But for this usage, it should be true or false. 
      $(this).dialog("close"); 
     }, 
     "Cancel": function() { 
      defer.resolve("false"); //this text 'false' can be anything. But for this usage, it should be true or false. 
      $(this).dialog("close"); 
     } 
     }, 
     close: function() { 
     //$(this).remove(); 
     $(this).dialog('destroy').remove() 
     } 
    }); 
} 
相關問題