2012-06-30 23 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