0
Good afternoon, I have a form, it has a text field that triggers the event . Blur that takes the name typed and returns an id with ajax, picks up the saved id in another Hidden input, the problem is that the Blur event is only triggered if you click in empty place (to lose focus) and then in the Submit button, if you click directly in the Submit the Blur event is not triggered, any solution to such a problem? I tried to switch to event change but it also didn’t work if you click directly on Submit.
$("#responsavel").blur(function(){var cdgResp = $("#responsavel").val();$.post("controller/responsavel.php", {nomeResponsavel:cdgResp} , function(idResp) {if (idResp != false){$("#idResponsavel").val(idResp); }else { $("#idResponsavel").val("0"); } });});
above is the javascript that fires the Blur
<form action="javascript:func()" method="post">action="javascript:func()" method="post"><input id="responsavel" type="text" name="responsavel" value=<?php echo $dados['responsavel']; ?>"><input id="idResponsavel" type="hidden" name="idResponsavel" value="<?php echo $dados['idResponsavel']; ?>"><input type="submit" value="enviar"></form>
and above this the form, I created now generic to exemplify better, the data is already filled with php but if change the name of the responsible field would have to change the id of the same.
You have to put the friendly code to get proper help!
– LeAndrade
Good morning, I changed the description exemplifying my problem.
– walter alves
Have you tried "onfocusout" ? Try looking at the js mailing list: https://www.w3schools.com/jsref/dom_obj_event.asp
– aa_sp
Actually the Blur event is actually being triggered, but as it is an AJAX that does the form Submit there is happening before AJAX is finished being processed, so not the time to fill in the Hidden field. Always remember that AJAX are requests in parallel. In your case I recommend using $.ajax instead of $.post and using the attribute
async: false
– Lucas Ferreira
I tried yes @andreia_sp , but it didn’t work.
– walter alves
@Lucasferreira is exactly what is happening, is shooting but the data going to Submit is not changed, thanks for the tip, I will try this way with async:false
– walter alves