featherlight.js - 使用 FeatherLight 打开后无法更改/获取输入值?
问题描述
我遇到了一个奇怪的行为。我正在尝试在FeatherLight 灯箱关闭后获取/设置输入文本框的值。尝试获取文本框的值时出现“未定义”(同样,在通过 Featherlight 打开和关闭它之后)。你可以在这里看到我的源代码:
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>
解决方案
第一次之后,DOM 元素保持分离(我打开以重新插入它……)。所以$('#MyTextBox')
以后就不行了。要么保留对它的引用,要么使用storeData.$content.find('#MyTextBox')
推荐阅读
- angular - 如何在ionic3中调用函数内部的函数?
- jersey-2.0 - 授权在招摇球衣中不起作用?
- android - 由于线程“main” java.util.zip.ZipException 中的异常,无法运行 react-native 应用程序:打开 zip 文件时出错
- mongodb - expressjs 不允许变量保留范围,即使它们不是本地的
- hibernate - JPA/Hibernate:在不存在的序列/关系上选择 currval
- ruby-on-rails - 如何在 rails api 中使用 devise-jwt 和 devise 进行登录、注册和注销
- crystal-reports-2010 - 如何避免水晶报表中的下一个整数?
- python - 如何在 Django 项目中启动 celery worker
- java - 从另一个类调用函数?
- swift - 键盘未关闭