简体   繁体   中英

Click event fires twice, but only in one case

can't answer yet so...

Found the problem...tried out everything you guys said and going line by line with firebug I found that when I .empty() a div with javascript, whatever js was in there stays on, so every time I refreshed the status pane, I added another group of js to the stack.

Have to better organize my js...thanks all.

//

I have some jquery that handles clicks on a control panel type system. There's three kinds of clicks, on/off (class = "checkbox"), modal pop-up (class="editbox") and the execution of the modal. "checkbox" and "editbox" work fine, either toggling the option or showing the pop-up, however, when the hasClass('fl_ejecuta') if matches, it is called twice.

I've gone over the code a number of times and even isolated it completely from everything else, yet that one section always gets called twice...can anyone see what I've missed? I can't get this to work for some reason.

Here is the relevant javascript:

$(".conclick").click(function() {
    var accion = $(this).attr('accion');
    var data = $(this).attr('data');
    var valor = $(this).attr('valor');
    var esto = $(this);
    if($(this).hasClass('checkbox')) {
        $.post("ejecuta.php",{t:"checkbox",a:accion,d:data,v:valor}).done(function(retorno) {
            if(retorno != "N") {
                if(valor == "1") { esto.attr('valor','0'); esto.empty(); }
                if(valor == "0") { esto.attr('valor','1'); esto.empty().html('✓'); }
            } else {
                hayError("checkbox",accion,data,valor);
            }
        });
    }
    if($(this).hasClass('editbox')) {
        $.post("ejecuta.php",{t:"editbox",a:accion,d:data,v:valor}).done(function(retorno) {
            if(retorno.status == "OK") {
                $('.fl_titulo').empty().html(retorno.titulo);
                $('.fl_explica').empty().html(retorno.explica);
                $('.fl_data').empty().html(retorno.form);
                $('.fl_ejecuta').attr('data',retorno.data);
                $('#flotante').show('fast');
            } else {
                hayError("editbox",accion,data,valor);
            }
        }, "json");
    }
    if($(this).hasClass('fl_ejecuta')) {
        alert('fl_ejecuta');
    }
});

And the HTML that fires the click twice:

    <div id="flotante" style="display:none;">
    <div id="flota">
        <div class="fl_titulo"></div><div id="fl_cerrar">X</div>
        <div class="fl_explica"></div>
        <div class="fl_data"></div>
        <div class="conclick fl_ejecuta" data="" valor=""><?php echo texto("MODIFICAR"); ?></div>
    </div>
</div>

Your code for

$(".conclick").click(function() {

seems to be working ok:

http://jsfiddle.net/kexwh/1/

Try to use:

$(".conclick").live('click',function() { ....

You are probably attaching click listener twice in your js. Or look at delegated .on.

Found the problem...tried out everything you guys said and going line by line with firebug I found that when I .empty() a div with javascript, whatever js was in there stays on, so every time I refreshed the status pane, I added another group of js to the stack.

Have to better organize my js...thanks all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM