This syntax is valid in jquery?

1

I have this code, so in javascript there should be a return true and return false, but in this statement there is only one false return, the return true is implicit?

if (_form.find ('# name'). val () === '') {             _form.find ('# name'). addClass ('u-validate');             return false;         };

this is the original function

	$("#btn-registrar").on('click',  function(event) {
		event.preventDefault();

		var _form = $("#form-add-marcas");

		if (_form.find('#name').val()==='') {
			_form.find('#name').addClass('u-validate');
			return false;
		};

		if (_form.find('#description').val()==='') {
			_form.find('#description').addClass('u-validate');
			return false;
		};


		$.ajax({
			url: baseURL +'marcas/action_add',
			type: 'POST',
			dataType: 'json',
			data: _form.serializeArray(),
		})
		.done(function(response) {
			
			if (Boolean(response.status)===true) {
				loadURL(baseURL+'marcas/list_marcas');

				//alert(response.message);
			}else{
				alert(response.message);
				_form.find('#'+response.id).addClass('u-validate');
			}
		})
		.fail(function(response) {
			console.log("error",response);
		})
		 
	});
    
asked by Kpeski2814 25.08.2018 в 16:19
source

2 answers

1

Ok, friend, now if I can answer you, I explain what your javascript does, when you give click to the button you are generating an OnClick event, what those do strong> if is that if they get the fields name and description empty they return a false in the function and cancel the OnClick event / strong>. You could say that by clicking the button you are sending a true Click = True and when returning the false you cancel that Click = False.

$("#btn-registrar").on('click',  function(event) {
  event.preventDefault();
  
  var _form = $("#form-add-marcas");
  
  if (_form.find('#name').val()==='') {
    _form.find('#name').addClass('u-validate');
    return false; //CANCELAR EL CLICK
  };
  
  if (_form.find('#description').val()==='') {
    _form.find('#description').addClass('u-validate');
    return false; //CANCELAR EL CLICK
  };
  
  $.ajax({
    url: baseURL +'marcas/action_add',
    type: 'POST',
    dataType: 'json',
    data: _form.serializeArray(),
  })
  .done(function(response) {
			
    if (Boolean(response.status)===true) {
      loadURL(baseURL+'marcas/list_marcas');
      //alert(response.message);
    }else{
      alert(response.message);
      _form.find('#'+response.id).addClass('u-validate');
    }
  })
  .fail(function(response) {
    console.log("error",response);
  })
});
    
answered by 25.08.2018 / 17:34
source
0

Without seeing the HTML is a little speculation, but ... if we assume that the button is within a FORM when you click the expected is that the FORM is sent.

The first instruction event.preventDefault(); (for this event to prevent the default action) avoids this behavior, then the conditions that return false cancel the click (the FORM is not sent) and the function ends there.

Later there is an ajax call that sends the information of FORM , so from that point it would not be necessary to return true , otherwise it would be resending the FORM : once per ajax the second once for the HTML .

    
answered by 25.08.2018 в 19:40