2015-07-20 29 views
-2

我創建了一個div。在這個div中,我添加了HTML按鈕點擊的文本框。還有另一個名爲「SHOW」的按鈕。點擊這個按鈕我想顯示整個這個div的內容。使用document.getElementByID('#div').innerHTML,我可以得到這個div內的HTML文本。如何獲得選定的DIV的HTML以及DIV內控件的指定值?

我的問題是當我在這個動態創建的文本框中鍵入內容,然後當我點擊顯示按鈕時,它不會在文本框中顯示輸入的文本。我的問題是,我怎樣才能得到在文本框中輸入的值整個div的HTML? 這裏是我的一小段代碼片段:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm9.aspx.cs" Inherits="E_FormDemo.WebForm9" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
<script src="http://code.jquery.com/jquery-1.8.2.js"></script> 
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script> 
<script> 
    $(document).ready(function() { 

     $('#Button1').click(function() { 
      AddText(); 
     }); 

     function AddText() { 
      var value; 
      value = '<input class="dvHover" id="Text" type="text">'; 
      $(value).appendTo('#div1'); 
     }; 

     $('#Button2').click(function() { 
      getText(); 
     }); 

     function getText() { 
      var s; 
      s = document.getElementById('div1').innerHTML; 
      alert(s); 
     }; 

    }); 

    </script> 
</head> 
<body> 
<form id="form1" runat="server"> 
    <input id="Button1" type="button" value="button" />&nbsp;&nbsp; 
    <input id="Button2" type="button" value="show" /></form> 
<div id="div1"> 

</div> 
</body> 

快速反應是非常明顯的。 感謝

+2

請給我們一些代碼!的jsfiddle? – DDan

+2

'getElementByID('#div')'無效。首先它是錯誤的大寫,第二,除非你使用jQuery或querySelector,否則不需要'#'。 – dave

+0

目前還不完全清楚你想要達到的目標。你有一個帶有輸入字段的DIV元素,你想獲得該DIV的HTML內容以及該DIV中的INPUT字段的值?聽起來像你做錯了什麼。考慮添加一些代碼並更好地解釋你想要達到的目標。 – martynasma

回答

1

我指派新的ID給每個文本框和文本框的內容的變化使用更新值:

變種V = $(本).VAL();

$(this).attr('value',v);

下面是更新後的代碼:

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
<script src="http://code.jquery.com/jquery-1.8.2.js"></script> 
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script> 
<script> 
    var i = 0; 
    $(document).ready(function() 
    { 


     $('#Button1').click(function() 
     { 
      AddText(); 
     }); 

     function AddText() 
     { 
      var value; 
      value = '<input class="dvHover" id="Text' + i++ + '" type="text" value="">'; 
      $(value).appendTo('#div1'); 
     }; 

     $('#Button2').click(function() 
     { 
      getText(); 
     }); 

     function getText() { 
      var s; 
      s = document.getElementById('div1').innerHTML; 
      alert(s); 
     }; 
    }); 

    $(document).on('change', '.dvHover', function() 
    { 
     var v = $(this).val(); 
     $(this).attr('value', v); 
    }); 
    </script> 
</head> 
<body> 
<form id="form1" runat="server"> 
    <input id="Button1" type="button" value="button" />&nbsp;&nbsp; 
    <input id="Button2" type="button" value="show" /></form> 
<div id="div1"> 

</div> 
</body> 
</html> 

希望這有助於!

+0

嗨,@Mojo_Manga,謝謝你的回覆。這個對我有用。再一次感謝你。 – Ankit