【问题标题】:Get Javascript value from Dynamically adding/removing input fields in JQuery从 JQuery 中动态添加/删除输入字段获取 Javascript 值
【发布时间】:2018-04-12 03:00:01
【问题描述】:

我关注这个链接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--;
    })
});

【问题讨论】:

    标签: javascript jquery html css node.js


    【解决方案1】:

    根据名称获取您的价值。

    例子:

    $('input[name^="mytext"]').each(function() {
        alert($(this).val());
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用选择器".input_fields_wrap input[name='mytext[]']" 获取所有动态&lt;input&gt; 元素,将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>

      【讨论】:

      • 非常感谢!这将如何在 node.js 服务器上工作?
      • node.js 与 HTML 文档中的元素有什么关系?
      猜你喜欢
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-08
      • 2013-04-28
      相关资源
      最近更新 更多