Function being executed twice jQuery

Asked

Viewed 835 times

2

I got the following jQuery:

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

    var id_cliente = $("#id_cliente").val();
    var id_pacote = $("#id_pacote").val();
    var tipo_reserva = $("#tipo_reserva").val();
    var data_evento = $("#data_evento").val();

    var itens = "";
    $('.checados').each(function(){
        var tmp = this.value.split("|");
        var v = tmp[1],
            result = 0;
        v = v.toFloat();            
        itens += v +";";
    });

    $.ajax({
        url: basePath + 'evento/salvar_festa',
        type: 'POST',
        dataType: 'html',
        data: {
            id_cliente: id_cliente,
            id_pacote: id_pacote,
            tipo_reserva: tipo_reserva,
            itens: itens,
            data_evento: data_evento
        },
    })
    .done(function(ret) {
        console.log("success");
        $('#mensagePage').html(ret);
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    }); 

});

It saves normal in the database, but saves duplicate. I put an Alert in the beginning, like this:

$("#salvar_festa").click(function() {
    alert('teste');

It appears two Alerts, when you click.

Can anyone tell me where the mistake is?

EDIT - JQUERY THAT CALLS PACKAGES

$(".pacote").change(function() {
    var pacote = $(".pacote").val();
    var data = $("#data").val();

    $.ajax({
        url: basePath + 'evento/get_pacotes_item',
        type: 'POST',
        dataType: 'html',
        data: {
            pacote: pacote,
            data: data
        },
    })
    .done(function(ret) {
        console.log("success");
        $('.pacotes_lista_itens').html(ret);
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });

});

EDIT HTML

<div class="row"> 
    <div class="col-md-12">
        <div class="row">
            <input type="hidden" id="data" name="data" value="<?php echo $_REQUEST['data']; ?>">
            <div class="col-md-6 form-group">
                <label>Selecione o Cliente</label>
                <select class="form-control" id="id_cliente" name="id_cliente">
                    <option value="0">Selcione um Cliente</option>
                    <?php foreach($lista_clientes as $valor){ ?>
                    <option value="<?php echo $valor->id; ?>"><?php echo $valor->nome_responsavel; ?></option>
                    <?php } ?>
                </select>
            </div>
            <div class="col-md-6 form-group">
                <label>Selecione o Pacote</label>
                <select class="form-control pacote" id="id_pacote" name="id_pacote">
                    <option value="0">Selcione um Pacote</option>
                    <?php foreach($lista_pacotes as $valor){ ?>
                    <option value="<?php echo $valor->id; ?>"><?php echo $valor->nome; ?></option>
                    <?php } ?>
                </select>
            </div>
            <div class="col-md-12">
                <div class="pacotes_lista_itens"></div>
            </div>  

            <div class="col-md-12 form-group">
                <br/>
                <button id="salvar_festa" class="btn btn-success">Salvar</button>
                <button onClick="close()" class="btn btn-warning">Cancelar</button>
            </div>
        </div>
    </div>
</div> 
  • Post the widget HTML

  • Are you sure that line $("#salvar_festa").click(function() { appears only once in the code?

  • For html....

  • @Sergio yes I’m sure, because I called the function in one place, and if I delete everything inside the function, and leave only the Alert, it duplicates the Alert also...

  • I also posted the jQuery that calls the page to complete the form.

  • 1

    Try giving a $("#salvar_festa"). off("click"); before assigning the click

  • 1

    It worked @Sorack thank you so much, saved the day by searching rsrs

  • 1

    I’m gonna post the answer so you can approve and help someone who’s looking for the same thing, okay?

  • Great, in 5 minutes I accept your answer. does not allow before, but gave very sure!

  • You can reproduce the problem in a jsFiddle?

  • Try using a Return before closing the click function. return false;

  • 1

    If it does not work, put the code that saves the info in the bank.

Show 7 more comments

1 answer

2


To avoid duplicate events use the following code before assigning the click:

$("#salvar_festa").off("click")

And try to assign the click as follows:

$(document).on("click", "#salvar_festa", clicar);
  • 1

    That doesn’t solve the problem, hide it.

  • Puts, @Sergio rs, now chipped so it might have something to do with angular js? because the system has this angular business but I don’t understand any of it :\

  • 1

    @Andrébaill something is duplicating that click. See in dev tools which "Event handlers" are attached to that element and which line of code they come from.

  • Where do I find this?

  • See, do what St said and take a look at the second part of the answer I gave

  • I’m trying to make sure I know exactly where this extra click comes from..

Show 1 more comment

Browser other questions tagged

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