jQuery 如何获取 Div 的所有文本框的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11271457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:25:17  来源:igfitidea点击:

How to get Value of All textbox of Div?

jquery

提问by Kaps Hasija

I have a div Container, and I am creating a dynamic textbox control when an Add button is clicked

我有一个 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">

I want to get the value of all textbox controls, eg:

我想获取所有文本框控件的值,例如:

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

回答by Sang Suantak

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

回答by Adil

This will give you $ separated concatenated value of all text boxes with div having id = "Container"

这将为您提供所有带有 id = "Container" 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);

回答by Sablefoste

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

output will be stored in the variable completetext

输出将存储在变量中 completetext