jquery - 在窗口中找不到元素
问题描述:
我使用这些元素 - jquery窗口。 http://fstoke.me/jquery/window/jquery - 在窗口中找不到元素
我创建窗口,我有一个网站创建div。这个div是隐藏的。当我打开窗口div是可见的。这工作正常。
当我点击图片(#imgChatSend) - 我想从输入(#txtChatInput)获取值。 条目始终为空。为什么? 我该如何解决这个问题?
谢谢您的建议!
这是我的代码:
HTML
<div id="ChatForm" style="display:none;">
<input id="txtChatInput"name="txtChatInput" type="text" />
<img id="imgChatSend" src="images/btn-send.png" alt="Send" />
</div>
JS
windowchat = $.window({
title: "Chat",
height: 300,
width: 300,
content: $("#ChatForm").html(),
minWidth: 300,
minHeight: 300,
maxWidth: 800,
maxHeight: 800
});
$('#imgChatSend').live('click', function() {
alert($("#txtChatInput").val()); //$("#txtChatInput").val() is ""
return false;
});
答
看来窗口插件创建你是显示在窗口中的内容的克隆,产生2 input
,其编号为txtChatInput
。
使用$("#txtChatInput")
将引用与该特定id(id应该是唯一的)一起找到的第一个元素。
一个解决办法是给你选择的上下文中,它会开始寻找输入:
$('#imgChatSend').live('click', function() {
var $parent = $(this).closest("div"); // get the div the button we just clicked is in
alert($("#txtChatInput", $parent).val()); // tell jquery to only look in this div for the element with id txtChatInput
return false;
});
谢谢!我真的有双重输入。整个html代码我写在js中。 – Jenan