用FeatherLight打开后无法更改/获取输入值? [英] Can't change / get value of input after being opened with FeatherLight?

查看:81
本文介绍了用FeatherLight打开后无法更改/获取输入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了奇怪的行为.我试图获取/设置输入文本框的值之后关闭FeatherLight灯箱.尝试获取文本框的值时(同样,通过Featherlight打开和关闭该文本框后),我得到未定义"的信息.您可以在此处查看我的源代码:

I've met a weird behavior. I'm trying to get / set the value of an input textbox After FeatherLight lightbox is closed. I get "Undefined" when trying to fetch the value of the textbox (again, after it was opened and closed via Featherlight). You can see my source code here:

https://jsfiddle.net/hgyba4dg/

唯一相关的部分是html代码.尝试在JSFiddle上运行代码,并查看输入的值(文本框的值).您将看到在关闭Featherlightbox后将得到未定义".这是html:

The only relevant part is the html code. try running the code on JSFiddle and see the input's value (the textbox's value). You'll see that you'll get "undefined" after closing the Featherlightbox. Here is the 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')

After the first time, the DOM elements remain detached (I open to reinsert it one day...). So $('#MyTextBox') won't work afterwards. Either keep a reference to it, or use storeData.$content.find('#MyTextBox')

这篇关于用FeatherLight打开后无法更改/获取输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆