Javascript-enabled checkbox for controller

Asked

Viewed 568 times

1

I have a group of checkboxes that are generated from the BD, therefore 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 to side the controller I can get the id of the checkbox selected and not knowing if one of the three is selected.

Edited:

If instead of:

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

Put:

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

always returns Check1.

Put:

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

returns null

  • All your checkboxes always have the same value as the "name attribute"?

  • The user can select none, one and/or more of a checkbox?

2 answers

0

Try (no space before the :checked). He’s gonna 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>

0

Taking into account that you are working with checkboxes and there may be more than one selected, it is interesting that you send 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;
            }()
    }
});

Since Java is not my strong suit, I believe I can do something similar: Java

System.out.println("Teste: " + String.join(", ", form.data().get("teste")));
  • Thanks for your help. In the ajax part, you don’t recognize the test: ids, it says: 'ids is not defined'. However if instead of ids, put any string by e.g. test: "example" in the controller it recognizes this String and prints it well!

  • Hugo, as I said Java is not my strong suit, so I’m not familiar with this call ajax, in any case try again (I just edited).

  • I believe so and in any case I appreciate your help. No longer an error, now simply println returns null. PS: Selection of a checkbox is only allowed. I don’t know if this detail can help

  • as only a checkbox can be checked, so your example is OK, you just need to change the following: data : { test: $("input[name=test] :"checked). attr("id") }

  • That way I keep getting null :s I must be missing something... Thanks for anything for the help

  • a conrrection: $("input[name='test'] :checked"). attr("id") without the quotation marks becomes difficult;D

  • I think the problem will not be there. It returned null again and I have cases where I do so: name: $("input[name=name]"). val() and passes well to the controller, tou is with difficulties with the checkboxes -.-

  • tries to inspect the checkbox in the browser, to see if the ID is normal.

  • I’m going to get them from the BD, I don’t know if you realized it, so the id is: [email protected](), but yes, in the browser, for every check I have: Check1 Check2 Check3, it’s hard xD

  • Remove the space before the ":" in $("input[name=teste]:checked").attr('id')

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.