Why does not a conditional work and execute the AJAX petition?

0

I have a code in which I check if the client in an input was introduced. If so, if this checking account is checked, write it down and it is because it was charged on the spot.

But first I have a conditional, where if the client code has not been introduced do not do anything.

 $("#canilla1").click(function(event) {

 $("input[value=Confirmar]").click(function(){
        
      var id = $(this).attr("id");

  
      var cliente = $("#codcli").val();
      
     if (cliente!==""){
        
        
      if ($("#cc").attr("checked",false)){ 
      pago="s";
        $.ajax({
	  
            type: "POST",
            url: "../gestionweb/views/modules/venta/procesoventa.php",
                data:{"accion":"cc","cliente":cliente,"idpg":id,"fecha":fecha,"hora":hora,"pago":pago},
      

            error: function(){
                alert("error petición ajax");
            },
            success:function(data)
            {
               window.location.href="index.php?controller=venta&action=index";
            }
            });
      } else if ($("#cc").attr("checked")){
         pago="n";
        $.ajax({
	  
            type: "POST",
            url: "../gestionweb/views/modules/venta/procesoventa.php",
                data:{"accion":"cc","cliente":cliente,"idpg":id,"fecha":fecha,"hora":hora,"pago":pago},
      

            error: function(){
                alert("error petición ajax");
            },
            success:function(data)
            {
               window.location.href="index.php?controller=venta&action=index";
            }
            });
      } }else {alert("ingrese el cliente");}
 
});});

But the fact is that it executes the ajax queries in the same way.

    
asked by Caruso 12.11.2018 в 19:02
source

2 answers

0

What the client validation is missing are the comparison operators, I suggest you use Inequality (! =)

Inequality (! =) Returns true if both operands are not equal.

Strictly unequal (! ==)
Returns true if the operands are not equal and / or are not of the same type.

    
answered by 13.11.2018 / 00:37
source
0

To validate the value of the ID field you can with $.trim( id ) example:

var id = $('.input').val();

if( $.trim( id ) ){
  // true
} else {
  // false
}

To validate the checkbox you can use $.('.checkbox').is(':checked') example:

if( $.('.checkbox').is(':checked') ){
  // true
} else {
  // false
}

The complete and functional code should be like this ...

$(document).ready(function(){

  $( '#send' ).click(function( event ){
  
    if( event ) event.preventDefault(); // Evita refrescar
  
    var clientID = $( '#client' ).val();
    
    // Con trim eliminamos espacios en blanco
    // Comprobamos que no tenga un valor:
    // TRUE: mostrar alerta
    // FALSE: nuna se ejecutará el codigo dentro de { ... }
    if( $.trim( clientID ) == "" ){
      alert('Introduce la ID del cliente');
      return;
    }
    
    // Con .is(':checked') Comprobamos si esta marcado o no 
    var CC = $( '#cc' ).is(':checked');
    
    if( CC ){
        
      // Hacemos un ajax si es correcto
      // Aqui deberia estar el ajax de tu codigo
      // con tus parametros
      // Esto es solo un ajax de prueba
      $.ajax({
        url: 'https://jsonplaceholder.typicode.com/posts/1',
        method: 'GET',
        dataType: 'json',
        success: function( response ){
            $('.reponse')
            .html('') /* Reseteamos*/
            .append('<p>CC es activado</p>')
            .append('<p>'+ clientID +' es la ID del client</p>')
            .append('<p>Respuesta del AJAX:</p>')
            .append(JSON.stringify( response ))
        }
      })
    
    } else {
      /* Si CC NO ES ACTIVADO */
    
       // Hacemos un ajax si es correcto
      // Aqui deberia estar el ajax de tu codigo
      // con tus parametros
      // Esto es solo un ajax de prueba
      $.ajax({
        url: 'https://jsonplaceholder.typicode.com/users',
        method: 'GET',
        dataType: 'json',
        success: function( response ){
            $('.reponse')
            .html('') /* Reseteamos*/
            .append('<p>CC NO es activado</p>')
            .append('<p>'+ clientID +' es la ID del client</p>')
            .append('<p>Respuesta del AJAX:</p>')
            .append(JSON.stringify( response ))
        }
      })
    
    }
    
  })
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <input type="text" id="client">
  <input type="checkbox" id="cc">
  <input type="submit" id="send" value="enviar">
</form>

<div class="reponse"></div>

I hope the answer to your question is correct.

    
answered by 12.11.2018 в 20:44