javascript - 从 JQuery 中动态添加/删除输入字段获取 Javascript 值

标签 javascript jquery html css node.js

我正在关注此链接 here

如何将所有文本框中的值获取到 JavaScript 表单中的数组中?我试图将它嵌套到一个表单中;但是,我无法获取 HTML ID,因为当我添加/删除 jQuery 文本框字段时它会不断变化

HTML

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

Javascript

$(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><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

最佳答案

您可以使用选择器".input_fields_wrap input[name='mytext[]']"获取所有动态<input>元素具有name属性设置为 "mytext[]"传递至document.querySelectorAll()Array.from()或者 jQuery()$.map()创建具有相同值的数组

let values = Array.from(
               document
               .querySelectorAll(".input_fields_wrap input[name='mytext[]']")
            , ({value}) => value);

console.log(values);
<div class="input_fields_wrap">
  <button class="add_field_button">Add More Fields</button>
  <div><input type="text" name="mytext[]" value="0"></div>
</div>
<div class="input_fields_wrap">
  <button class="add_field_button">Add More Fields</button>
  <div><input type="text" name="mytext[]" value="1"></div>
</div>
<div class="input_fields_wrap">
  <button class="add_field_button">Add More Fields</button>
  <div><input type="text" name="mytext[]" value="2"></div>
</div>

let values = $.map($(".input_fields_wrap input[name='mytext[]']")
            , ({value}) => value);

console.log(values);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class="input_fields_wrap">
  <button class="add_field_button">Add More Fields</button>
  <div><input type="text" name="mytext[]" value="0"></div>
</div>
<div class="input_fields_wrap">
  <button class="add_field_button">Add More Fields</button>
  <div><input type="text" name="mytext[]" value="1"></div>
</div>
<div class="input_fields_wrap">
  <button class="add_field_button">Add More Fields</button>
  <div><input type="text" name="mytext[]" value="2"></div>
</div>

关于javascript - 从 JQuery 中动态添加/删除输入字段获取 Javascript 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47026979/

相关文章:

javascript - WebGL 对象是否被垃圾收集?

javascript - 如何删除类并添加另一个类jquery

jquery - 无法使用 JQuery 且 CSS 功能非常有限时的固定表头策略

php - 将数据从 PHP 传输到 Javascript 有哪些最佳实践?

javascript - Node child_process.exec : Disable printing of stdout on console

javascript - Discord JS(机器人存在)

javascript - 如何切换/折叠 javascript 元素(我想默认折叠 html)

Javascript:单击 div/form 中的按钮不执行针对它的 javascript?

html - Internet Explorer 8 和 Firefox 14 的布局差异

html - 如何使 anchor 标记内的图像可访问?