1

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

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>
4

5 回答 5

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?
        }
    });

但是,如果您也能找到根本原因,那就更好了。

于 2012-11-29T09:18:25.800 回答
0

你可以通过在 jquery 中解除绑定来做到这一点

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

});
于 2012-11-29T09:16:07.257 回答
0

使用 Jquery .one()方法,这就是它的用途。

 $('img.delete'.one('click',function () {         
            if(confirm("This selected record will be deleted. Are you sure?")){
                //Do something...
            }
        });        
于 2012-11-29T09:17:12.230 回答
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);
于 2012-11-29T09:18:45.487 回答
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>
于 2012-11-29T09:21:25.897 回答