OnClick我想弹出一个对话框按钮,该对话框要有一个uniqueId并在其中有一个iframe。这是我的代码:
$( ".insertDetailsDiv" ).click(function() {
var idName = $(this).attr("id");
var idNameBase = idName.replace('DetailsButton', '');
var iframeSRC = idNameBase+".php";
$("<div class='idNameDialogDivClass'></div>").dialog();
$(".idNameDialogDivClass").each(function() {
$(this).uniqueId();
var idNameDialogDiv = $(this).attr("id");
console.log(idNameDialogDiv);
});
$("<iframe></iframe>", {
id: idNameBase,
src: iframeSRC
}).appendTo('#'+idNameDialogDiv);
});
但是我的控制台日志(chrome)输出
ui-id-6
Uncaught ReferenceError: idNameDialogDiv is not defined
我不明白,控制台日志会输出该变量,然后javascript无法识别该变量。
最佳答案
尝试在idNameDialogDiv
函数之外声明该变量.each()
,
$( ".insertDetailsDiv" ).click(function() {
var idName = $(this).attr("id");
var idNameBase = idName.replace('DetailsButton', '');
var idNameDialogDiv = null; // Declare here.
var iframeSRC = idNameBase+".php";
$("<div class='idNameDialogDivClass'></div>").dialog();
$(".idNameDialogDivClass").each(function() {
$(this).uniqueId();
idNameDialogDiv = $(this).attr("id");
console.log(idNameDialogDiv);
});
$("<iframe></iframe>", {
id: idNameBase,
src: iframeSRC
}).appendTo('#'+idNameDialogDiv); // This was the place where the error was caused
});