A multi-dimensional array of flags?

Possible duplicate:
extracting a multidimensional array flag in javascript

Is it possible to implement a multi-dimensional array of flags?

for instance

<input type='checkbox' name='question[0][]' value='0'> <input type='checkbox' name='question[0][]' value='1'> <input type='checkbox' name='question[0][]' value='2'> <input type='checkbox' name='question[1][]' value='0'> <input type='checkbox' name='question[1][]' value='1'> <input type='checkbox' name='question[1][]' value='2'> <input type='checkbox' name='question[2][]' value='0'> <input type='checkbox' name='question[2][]' value='1'> <input type='checkbox' name='question[2][]' value='2'> 

If possible, how could you check if checkboxes are checked or not in javascript?

+4
source share
1 answer

Well, you can select a specific checkbox.

You can select the name question[x][] attribute question[x][] , and then loop through those to get each of their verified values.

An example of using jQuery:

 var checkedBoxes = {0: [], 1: [], 2: []}; $("input[name='question[0][]']").each(function(){ checkedBoxes[0].push(this.checked); }); //then do the same for 1 and 2 //after everything: console.log(checkedBoxes); //a multidimesional array of checked boxes 

Or make it even more attractive:

 var checkedBoxes = {0: [], 1: [], 2: []}; for(index in checkedBoxes) { $("input[name='question[" + index + "][]']").each(function(){ checkedBoxes[index].push(this.checked); }); } //after everything: console.log(checkedBoxes); //a multidimesional array of checked boxes 

Fiddle: http://jsfiddle.net/maniator/XA8XV/

+4
source

Source: https://habr.com/ru/post/1448091/


All Articles