Check checkbox checked [duplicated]

1

Good, I have put a small questionnaire and in the end I have a checkbox to accept the subject of privacy policies. But I can not check whether they have selected it or not.

I would like an alert to come out when it has NOT been selected

$(document).ready(function() {

  var estructuraMail = /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/;

  $('#Btn-Contacto-Enviar').click(function() {

    var nombre = $('#Nombre').val();
    var apellidos = $('#Apellidos').val();
    var mail = $('#Mail').val();
    var telefono = $('#Telefono').val();
    var comentario = $('#Comentario').val();

    var checkLOPD = $("#Checkbox-lopd:checked");

    if (nombre == "") {
      alert("El campo nombre es obligatorio");
    } else {
      if (apellidos == "") {
        alert("El campo apellidos es obligatorio");
      } else {
        if (mail == "") {
          alert("El campo e-Mail es obligatorio");
        } else {
          if (!estructuraMail.test(mail)) {
            alert("El correo electrónico no es válido");
          } else {
            if (telefono == "") {
              alert("El campo teléfono es obligatorio");
            } else {
              if (isNaN(telefono)) {
                alert("El numero de teléfono ha de ser numérico");
              } else {
                if (comentario == "") {
                  alert("¡Cuéntame algo!");
                } else {
                  if( $('#Checkbox-lopd').is(':checked') ) {
                    alert('Seleccionado');
                  }
                }
              }
            }
          }
        }
      }
    }
  });
});

Greetings!

    
asked by NEA 21.09.2017 в 21:12
source

3 answers

3

As I see you use JQuery you can also try:

$('#Checkbox-lopd').is(':checked')

or maybe you see

$('#Checkbox-lopd').attr('checked')

both return true or false

    
answered by 21.09.2017 в 21:17
1

You can use it is available from version 1.6 onwards of jquery

if( $('.micheckbox').prop('checked') ) {
alert('Seleccionado');

}

or from previous versions:

if( $('.micheckbox').attr('checked') ) {
alert('Seleccionado');

}

I leave more information about this: link

    
answered by 21.09.2017 в 21:19
1

You are great with using:

if( $('#Checkbox-lopd').is(':checked') ) {
    alert('Seleccionado');
}

Greetings.

    
answered by 21.09.2017 в 21:25