Unread array with index defined in input

1

see below the following code:

<input type="text" id="task" name="task[]" value="100" />
<input type="text" id="task" name="task[]" value="110" />
<input type="text" id="task" name="task[]" value="120" />

And in jquery, I read the array normally:

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

alert(values);
});

Now, when you include the manual index in the array, it looks like this:

<input type="text" id="task" name="task[1]" value="100" />
<input type="text" id="task" name="task[2]" value="110" />
<input type="text" id="task" name="task[3]" value="120" />

In this case, jquery no longer reads the array. What should be done to make reading possible?

    
asked by anonymous 19.09.2016 / 16:15

1 answer

1

In this scenario I see no sense in using the index on the name property, since there is another problem with this HTML . You're giving% s of% to more than one element on your page. id , as its name says, is a unique identifier, should not have more than one on the same page. It would make more sense for you to work as follows:

<input type="text" id="task[1]" name="task[]" value="100" />
<input type="text" id="task[2]" name="task[]" value="110" />
<input type="text" id="task[3]" name="task[]" value="120" />

If you do not want to work directly with id , you should also remove this information. If something is not going to be used, do not put this information unnecessarily trying to predict its future use, this is not recommended (there is a principle that is spoken of: #

    

19.09.2016 / 18:25