How to get an Array with jQuery, multiple <input> with the same name

前端 未结 9 640
既然无缘
既然无缘 2020-11-28 19:02

I have a form where users can add input fields with jQuery.


After submitting the

相关标签:
9条回答
  • 2020-11-28 19:18

    HTML:

    <input type="text" name="task[]" class="form-control" id="task">
    

    JS:

    var tasks= new Array();
    $('input[name^="task"]').each(function() 
    {
    tasks.push($(this).val());
    });
    
    0 讨论(0)
  • 2020-11-28 19:21

    To catch the names array, i use that:

    $("input[name*='task']")
    
    0 讨论(0)
  • 2020-11-28 19:22

    Using map:

    var values = $("input[id='task']")
                  .map(function(){return $(this).val();}).get();
    

    If you change or remove the id (which should be unique), you may also use the selector $("input[name='task\\[\\]']")

    Working example: http://jsbin.com/ixeze3

    0 讨论(0)
  • 2020-11-28 19:22

    For multiple elements, you should give it a class rather than id eg:

    <input type="text" class="task" name="task[]" />
    

    Now you can get those using jquery something like this:

    $('.task').each(function(){
       alert($(this).val());
    });
    
    0 讨论(0)
  • 2020-11-28 19:27

    if you want selector get the same id, use:

    $("[id=task]:eq(0)").val();
    $("[id=task]:eq(1)").val();
    etc...
    
    0 讨论(0)
  • 2020-11-28 19:35

    Q:How to access name array text field

    <input type="text" id="task" name="task[]" />
    

    Answer - Using Input name array :

    $('input[name="task\\[\\]"]').eq(0).val()
    $('input[name="task\\[\\]"]').eq(index).val()
    
    0 讨论(0)
提交回复
热议问题