![](/img/trans.png)
[英]$(document).ready(function(){} jquery is getting called twice?
[英]Function cannot be called in $(document).ready(function() {
問題更多是調試/語法錯誤而不是方法。
我在外部 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() {
; 我得到一個Uncaught Reference Error 中的$(document).ready(function() {
。
所有必需的文件都已包含在調用腳本中。 我想了解這是為什么以及如何解決問題?
除了最后缺少花括號,並假設您的“必要文件”包括 jquery-ui,您的函數似乎沒有任何問題。 請參閱此jsfiddle ,它不會產生任何錯誤。
也許問題出在您代碼的其他地方? 如果您可以發布一個最小、完整和可驗證的示例,可能會有所幫助。
參考:
<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()
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.