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){
What is the correct way to configure the Idle value in element.val?
– Gabriel Midão
I will update the answer with an example.
– Keven Carneiro
Done. If you don’t understand something, don’t hesitate to ask.
– Keven Carneiro
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?
– Gabriel Midão
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
– Gabriel Midão
Puts a
console.log(data);
in thesuccess
from AJAX and puts what it returned, please this gives a much better sense of how the data is coming from the server.– Keven Carneiro
{Cdgroup: "1", Groups: "Teste123", Inactive: "0"} 0 because the checkbox is unchecked
– Gabriel Midão
Apparently it is returning as string, so it is necessary to do the
if
s with string also.element.val() == "-1"
andelement.val() == "0"
– Keven Carneiro
Let’s go continue this discussion in chat.
– Gabriel Midão