防止$('img')。点击触发几次

时间:2012-11-29 09:07:10

标签: javascript jquery

我想知道如何防止多次提示确认消息,提前感谢!

javascript

$('img.delete').click(function () {         
    if(confirm("This selected record will be deleted. Are you sure?")){
        //Do something...
    }
});

HTML

<tbody>
    <tr>
        <td><img class="delete" src="/images/delete.gif"><input value="708" type="hidden"></td>
        <td>aaa</td>
    </tr>
    <tr>
        <td><img class="delete" src="/images/delete.gif"><input value="595" type="hidden"></td>
        <td>bbb</td>
    </tr>
    <tr>
        <td><img class="delete" src="/images/delete.gif"><input value="19" type="hidden"></td>
        <td>ccc</td>
    </tr>
</tbody>

5 个答案:

答案 0 :(得分:2)

代码中的问题是您多次应用单击处理程序;从您发布的代码中看不到这一点,但这绝对是原因。

如果多次运行$('img.delete').click(fn),则不会覆盖以前的点击处理程序;相反,它增加了另一个处理程序之后,当单击img.delete时,将连续调用所有已注册的处理程序,从而生成一个弹出窗口,后跟另一个窗口。

应首先致电.unbind()来补救;这样做会有效地替换以前的点击处理程序。

$('img.delete')
    .unbind('click')
    .click(function () {         
        if(confirm("This selected record will be deleted. Are you sure?")){
            //Do something...

            // remove the image here I guess?
        }
    });

但是,如果你能够追捕根本原因会更好。

答案 1 :(得分:0)

你可以通过解析jquery

来做到这一点
$(function(){
    $('img.delete').click(function () {         
        if(confirm("This selected record will be deleted. Are you sure?")){
            //Do something...
        }
        $('img.delete').unbind('click');
    });

});

答案 2 :(得分:0)

使用Jquery .one()方法,这就是为此做的。

 $('img.delete'.one('click',function () {         
            if(confirm("This selected record will be deleted. Are you sure?")){
                //Do something...
            }
        });        

答案 3 :(得分:0)

如果您担心这一点,您可以确保在点击后没有事件处理程序:

var confirm_me=function () {
    if(confirm("This selected record will be deleted. Are you sure?")){
        //Do something...
    }
    //restore event handler
    $(this).one('click',confirm_me);
    //prevent event propagation
    return false;
    }

//BE sure that you not do that in loop. if possible do - see second code
$('img.delete').one('click',confirm_me);

<小时/> 其他情况:

var confirm_me=function () {
     var t=$(this);
    //check processing flag
    if (t.data('processing')) return false;
    //Set processing flag
    t.data('processing',1);


    if(confirm("This selected record will be deleted. Are you sure?")){
        //Do something...
    }

    //restore event handler
    t.one('click',confirm_me);
    //remove processing flag
    t.data('processing',0);
    //prevent event propagation
    return false;
    }

$('img.delete').one('click',confirm_me);

答案 4 :(得分:0)

使用它,我已经验证了。

<script type="text/javascript">
var check = false;
$(document).ready(function () {
        $('img.delete').click(function () {
        if (check != true) {
            if (confirm("This selected record will be deleted. Are you sure?"))
                check = true;
        }
        else {
            alert('you have deleted one');
        }
        });
     });
</script>