Checkbox selected javascript for controller

0

I have a group of checkboxes that are generated from the DB, consequently their id will be different. In my case 3 checkboxes are generated:

HTML:

<input type="checkbox" id="[email protected]()" name="teste" class="checkbox style-0">
<span>@tpEnt.getDescricao()</span>

JavaScript

myJSRoutes.controllers.---Controller.addExemplo().ajax({
                            data : {
                                teste: $("input[name=teste] :checked").val() 
                            }
                        });

Controller (Java)

System.out.println("Teste: " + form.data().get("teste"));

output: 'ON' (when one of the 3 checkboxes is selected). I would like to know how on the controller side I can get the id of the checkbox selected and not know if one of the three is selected.

Edited:

If instead of:

teste: $("input[name=teste] :checked").val()

If you put:

 teste: $("input[name=teste]").attr('id')

always returns Check1.

If you put:

teste: $("input[name=teste] :checked").attr('id') 

returns null

    
asked by anonymous 04.02.2015 / 16:34

2 answers

0

Considering that you are working with checkboxes and there may be more than one selected, it is interresting that sends an array containing the selected ids.

HTML

<input id="ckUm" data-type="teste" type="checkbox" checked />
<input id="ckDois" data-type="teste" type="checkbox" />
<input id="ckTres" data-type="teste" type="checkbox" checked />

JavaScript

myJSRoutes.controllers.---Controller.addExemplo().ajax({
    data : {
        teste: function () {
                var checkBoxes = $(":input[data-type='teste']:checked");
                var ids = $.map(checkBoxes, function (checkBox, indice) {
                    return $(checkBox).attr("id");
                });
                return ids;
            }()
    }
});

As Java is not my strong, I believe I can do something of the genre: Java

System.out.println("Teste: " + String.join(", ", form.data().get("teste")));
    
04.02.2015 / 17:15
0

Try (no space before :checked ). It will get the id of the selected box:

<input id="ck1" name="teste" type="checkbox" />
<br>
<input id="ck2" name="teste" type="checkbox" checked />
<br>
<input id="ck3" name="teste" type="checkbox" />

<script>
    myJSRoutes.controllers.---Controller.addExemplo().ajax({
      data : {
        teste: $("input[name=teste]:checked").attr('id')
      }
    });
</script>
    
12.08.2017 / 02:02