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

前端 未结 9 641
既然无缘
既然无缘 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:36

    You can't use same id for multiple elements in a document. Keep the ids different and name same for the elements.

    <input type="text" id="task1" name="task" />
    <input type="text" id="task2" name="task" />
    <input type="text" id="task3" name="task" />
    <input type="text" id="task4" name="task" />
    <input type="text" id="task5" name="task" />
    
    var newArray = new Array();
    
    $("input:text[name=task]").each(function(){
        newArray.push($(this));
    });
    
    0 讨论(0)
  • 2020-11-28 19:39

    You can use jquery.serializeJSON to do this.

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

    Firstly, you shouldn't have multiple elements with the same ID on a page - ID should be unique.

    You could just remove the id attribute and and replace it with:

    <input type='text' name='task'>
    

    and to get an array of the values of task do

    var taskArray = new Array();
    $("input[name=task]").each(function() {
       taskArray.push($(this).val());
    });
    
    0 讨论(0)
提交回复
热议问题