run automatically

Asked

Viewed 916 times

2

I have a code where I will need to have a processing page, and on this page I will need to run a javascript Function automatically.

Currently, I run this Function by clicking, like this:

<input type="button" id="listarFipe" value="Listar Veiculos Fipe" />

my job:

$(function() {
    $('#listarFipe').click(function() {
        var categorias = new Array();
        var carros = new Array();
        var potencia = new Array();
        var valvulas = new Array();
        var filtroMotor = new Array();
        var filtroValvula = new Array();

            //select montadora
              mont = document.form.montadora.selectedIndex; 
              montadora = document.form.montadora[mont].value; 

              AnoI = document.form.AnoInicial.selectedIndex; 
              AnoInicial = document.form.AnoInicial[AnoI].value;

              AnoF = document.form.AnoFinal.selectedIndex; 
              AnoFinal = document.form.AnoFinal[AnoF].value;


            //alert(j);



        $('input[id=montadora]:checked').each(function() {
            categorias.push($(this).val());
        });
        $('input[id=veiculo]:checked').each(function() {
            carros.push($(this).attr('class'));
        });
        $('input[id=potencias]:checked').each(function() {
            potencia.push($(this).attr('class'));
        });
        $('input[id=valvulas]:checked').each(function() {
            valvulas.push($(this).attr('class'));
        });     

        $('input[id=filtroMotor]:checked').each(function() {
            filtroMotor.push($(this).val());
        });
        $('input[id=filtroValvula]:checked').each(function() {
            filtroValvula.push($(this).val());
        });     

        //alert('getModeloFipe.php?montadora='+montadora+'&veiculos='+carros+'&motor='+potencia+'&valvulas='+valvulas+'&anoini='+AnoInicial+'&anofim='+AnoFinal+'&filtroMotor='+filtroMotor+'&filtroValvula='+filtroValvula);
        var url='getModeloFipe.php?montadora='+montadora+'&veiculos='+carros+'&motor='+potencia+'&valvulas='+valvulas+'&anoini='+AnoInicial+'&anofim='+AnoFinal+'&filtroMotor='+filtroMotor+'&filtroValvula='+filtroValvula;
        /*
        var url ='getModeloFipe.php?montadora='+montadora+'&veiculos='+carros+'&motor='+potencia+'&valvulas='+valvulas+'&AnoInicial='+AnoInicial+'&AnoFinal='AnoFinal;
        */
        //enviar para url ajax
          $.get(url, function(dataReturn) {
          $('#checkVeiculosFipe').html(dataReturn);
        });
    });
});

I tested a possible solution I found on the net:

$( "#assinar" ).trigger( "click" );

Or change your javascript click function from:

$("#assinar").click(function(){
...

To:

function processaAssinar() {
...

and call the function processaAssinar() right after you finish your processing.

  • You mean you want to run that anonymous function that the click makes run, but when the page loads and without being dependent on a click?

  • yes correct, this function is on a registration edit page, I need it to be loaded automatically

  • 1

    So just remove $('#listarFipe').click( and the last ) just staying: function processaAssinar() { instead of $('#listarFipe').click(function() {. You already tested that?

  • 1

    Put the code you made in jsFiddle. I don’t see it in your comment...

  • I had already tested, but I tested again getting like this: <pre>Function listarFipe() { </pre> and tested like this also <pre>$(Function() { Function listarFipe() {</pre> I need to put this function to automatically load on the page, not needing to click to call it

  • http://jsfiddle.net/inforicky/u7qhf6ra/

  • 1

    Okay, so just run/call the function... joining listarFipe(); at the end. -> http://jsfiddle.net/u7qhf6ra/1/

  • You have the option to exchange your Function for $(Document). ready(Function(){...}). Function will be executed as soon as the document is loaded

  • 2

    Doubt solved

Show 4 more comments

1 answer

1


Use a Module Pattern, which consists in placing its function between (), and then put it on the end again () but empty, follows an example:

Normal execution:

function exemplo() {
 alert('exemplo');
} 

exemplo();

Using Module Pattern:

(function exemplo() {

 alert('exemplo');

})()

This way it will become self-executable.

  • the appeal Module Pattern is also known as self executing Anonymous Function

Browser other questions tagged

You are not signed in. Login or sign up in order to post.