2012-06-30 22 views
2

我有一個div容器,和我創建一個Add按鈕被點擊時如何獲取Div的所有文本框的值?

<div id="Container"> 
    <input type="Submit" id="AddTextBox" value="Add"> 
    <!-- Here are my dynamic textboxes --> 
    <input type="text" value='' class="dynamic"> 
    <input type="text" value='' class="dynamic"> 
    <input type="text" value='' class="dynamic"> 
</div> 

<input type="create" id="create" onclick="GetValue();" value="create"> 

我想所有的文本框控件的值,例如動態文本框控件:

Function GetValue() 
{ 
    var COntain=TextBoxValue+"$"+Textbox2Value+"$"; // so on 
} 

回答

9
function GetValue(){ 
    var Contain = ""; 
    $("#Container :text").each(function(){ 
     Contain += $(this).val() + "$"; 
    }); 
} 
+0

感謝您的簡短工作代碼 –

2

這會給你的所有文本框$​​分開連接的值與ID的div =「容器」

Live Demo

str = ""; 
$('#Container input[type=text]').each(function(){ 
    str+=$(this).val() + "$"; 
}); 

//To remove the extra $ at end 
if(str != "") 
    str = str.substring(0,str.length-1); 
+0

1,在其他示例逗號不會被除去。 – ALH

0
var completetext =''; 
$('.daynamic').each(function() { 
    completetext = completetext + ', ' +$(this).val(); 
}); 

輸出將被存儲在變量completetext