Javascript 如何使用jquery获取动态添加的输入框的输入值

Javascript 如何使用jquery获取动态添加的输入框的输入值,javascript,jquery,html,jquery-ui,Javascript,Jquery,Html,Jquery Ui,我试图在单击时添加动态字段,并希望获取用户键入的每个输入框值。这是我的工作代码。 HTML代码 <div class="input_fields_wrap"> <div><input type="text" name="mytext"> <button class="add_field_button">Add More Fields</button> </div> </div> 添加更多字段 JS代码 $

我试图在单击时添加动态字段,并希望获取用户键入的每个输入框值。这是我的工作代码。 HTML代码

<div class="input_fields_wrap">
 <div><input type="text" name="mytext"> <button class="add_field_button">Add More Fields</button>
</div>
</div>

添加更多字段
JS代码

$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
   var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><tr><td><input type="text" name="textbox' + x + '" id="removeId' + x + '" value="' + x + '" ></td><td><button class="remove_field" >Remove</button><button class="save_field" >save</button></td></tr></div>'); //add input box

        }
    });
 $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); 
        $(this).parent('div').remove(); 
        x--;
    })
    $(wrapper).on("click",".save_field", function(e){ //user click on remove text
        e.preventDefault(); 
        //Here on click of each save button I wanna grab particular text box value that user types in
})
});
$(文档).ready(函数(){
var max_fields=10;//允许的最大输入框数
var wrapper=$(“.input_fields_wrapp”);//字段包装器
var add_button=$(“.add_字段_button”);//添加按钮ID
var x=1;//初始文本框计数
$(添加按钮)。单击(函数(e){//在添加输入按钮上单击
e、 预防默认值();
如果(x
每次单击“添加更多字段”时,我都会得到新的文本框以及“删除和保存”按钮,单击“保存”按钮时,我想获取用户键入的相应文本框值


单击的按钮是所需输入标记的同级。您可以使用
.sides('input')
.prevAll('input')
将输入元素与
.val()
一起作为目标,以获取其值:

$(this).siblings('input').val();
处理程序:

$(wrapper).on("click",".save_field", function(e){ //user click on remove text
    e.preventDefault(); 
    alert($(this).siblings('input').val());
    //Here on click of each save button I wanna grab particular text box value that user types in
});

您可以为文本框提供动态id替换:

id="removeId'

然后您可以使用

$("#id").val();

PFB代码,希望有帮助

$(文档).ready(函数(){
var max_fields=10;//允许的最大输入框数
var wrapper=$(“.input_fields_wrapp”);//字段包装器
var add_button=$(“.add_字段_button”);//添加按钮ID
var x=1;//初始文本框计数
$(添加按钮)。单击(函数(e){//在添加输入按钮上单击
e、 预防默认值();
如果(x

添加更多字段
$("#id").val();