Checkbox checked/unchecked when receiving 0/-1 from server

Asked

Viewed 215 times

0

I have a checkbox inside a modal that passes to the server by clicking a button the value 0 if it is not checked or the value -1 if it is checked. Now I am trying to do the reverse process, if it is recorded 0 when the page loads the checkbox should be unchecked, if it is recorded -1 it should be checked but I am not able to have this return, can someone give me a light? I’m learning javascript.

An example of JSON that is sent to the server:

{
Cdgrupo: 2,
Grupos: "grupo 2",
Dtalter: "2019-05-28T14:49:03.000Z",
Inativo: -1
},
{
Cdgrupo: 3,
Grupos: "grupo 3",
Dtalter: "2019-05-28T13:58:04.000Z",
Inativo: 0
},

This is the checkbox:

<input type="checkbox"  class="check" id="checkfor" style="position:absolute; left:330px; top:170px;" ></input>

And this is the javascript I tried to use to make the return:

$('#checkfor').ready(function() {
    if ($(this).attr('value', '-1')){
                 $(this).prop('checked', true);
             }
             else
              if ($(this).attr('value', '0')){
                 $(this).prop('checked', false);
             }
});

What I did wrong?

Follow a template of how I am making a request in ajax to get:

//Get de grupo
    $.ajax({
        url: "http://localhost:8081/datasnap/rest/TCadastros/GetListaGrupos//100/" + pag,       
        dataType: 'json',
        type: 'get',
        cache:false,
        success: function(data){

1 answer

2

In my view you are using the wrong event... using the ready Javascript will execute the function when the HTML is mounted on the screen. The problem is that at this time you still do not have the data of your request. The time to update this checkbox would be on success of the AJAX function.

It would look like this:

$.ajax({
    url: "http://localhost:8081/datasnap/rest/TCadastros/GetListaGrupos//100/" + pag,       
    dataType: 'json',
    type: 'get',
    cache:false,
    success: function(data) {
        var element = $('#checkfor');
        element.val(data.Inativo); // Aqui você configura o valor

        // Com o valor já atribuído, você faz as modificações
        if (element.val() == -1) {
            element.prop('checked', true);
        }
        else if (element.val() == 0) {
            element.prop('checked', false);
        }
    }
});

Below is an example. In your case, you could call the function changeElement in the success ajax.

// Begin mock
var data = {
    Cdgrupo: 2,
    Grupos: "grupo 2",
    Dtalter: "2019-05-28T14:49:03.000Z",
    Inativo: -1
};

changeElement(data);
// End mock

function changeElement(data) {
  var element = $('#checkfor');
  element.val(data.Inativo); // Aqui você configura o valor

  // Com o valor já atribuído, você faz as modificações
  if (element.val() == -1) {
      element.prop('checked', true);
  }
  else if (element.val() == 0) {
      element.prop('checked', false);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="checkbox"  class="check" id="checkfor" style="position:absolute; left:330px; top:170px;" ></input>

  • What is the correct way to configure the Idle value in element.val?

  • I will update the answer with an example.

  • 1

    Done. If you don’t understand something, don’t hesitate to ask.

  • I don’t understand this mock / end mock. I inserted the changeElement function inside the Success and the result was the same when I put only the first solution, all checkboxes are marked now. Under (date.Inactive) do I need to set some value? If yes how do I set up?

  • I hadn’t seen that you had updated if and Else, I made the change here and it hasn’t worked yet, but instead of all checkboxes appearing marked now are unchecked

  • Puts a console.log(data); in the success from AJAX and puts what it returned, please this gives a much better sense of how the data is coming from the server.

  • {Cdgroup: "1", Groups: "Teste123", Inactive: "0"} 0 because the checkbox is unchecked

  • Apparently it is returning as string, so it is necessary to do the ifs with string also. element.val() == "-1" and element.val() == "0"

Show 4 more comments

Browser other questions tagged

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