我遇到了一种奇怪的行为。我正在尝试获取/设置输入文本框的值 关闭FeatherLight灯箱后。当我尝试获取文本框的值时(在通过Featherlight打开和关闭它之后),我得到“Undefined”。你可以在这里看到我的源代码:
https://jsfiddle.net/hgyba4dg/
唯一相关的部分是html代码。尝试在JSFiddle上运行代码并查看输入的值(文本框的值)。关闭Featherlightbox后,您会看到“未定义”。这是html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
var storeData = null;
function openFeatherLight()
{
if (!storeData)
{
storeData = $.featherlight($('#MyDiv'), { 'persist' : true , 'beforeOpen' : sayHi , 'beforeClose' : sayBye, 'afterClose' : changeInputValueAfterClose });
}
else
{
storeData.open();
}
}
function sayHi()
{
alert('Value of textbox BEFORE opening FeatherLight LightBox Is: ' + $('#MyTextBox').val());
}
function sayBye()
{
alert('Value of textbox BEFORE CLOSING the FeatherLight LightBox Is: ' + $('#MyTextBox').val());
}
function changeInputValueAfterClose()
{
$('#MyTextBox').val("Bla");
alert('Current Value Of Input TextBox After Change is: ' + $('#MyTextBox').val());
alert('We get "Undefined". Why?');
}
</script>
</head>
<body>
<div id="Shit" style="display: none;">
<div id="MyDiv">
Text
<form id="MyForm">
<input type="text" value="Initial_Value_Set_By_Me_For_Testings" id="MyTextBox">
</form>
</div>
</div>
<input name="Button1" type="button" value="Open FeatherLight" class="AddToMenuButton" onclick="openFeatherLight();">
</body>
</html>
答案 0 :(得分:1)
第一次之后,DOM元素仍然分离(有一天我打开它重新插入......)。因此$('#MyTextBox')
之后无效。要么保留对它的引用,要么使用storeData.$content.find('#MyTextBox')