2011-09-24 72 views
0

我使用這些元素 - 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; 
      }); 

回答

1

看來窗口插件創建你是顯示在窗口中的內容的克隆,產生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; 
}); 
+0

謝謝!我真的有雙重輸入。整個html代碼我寫在js中。 – Jenan