Intercepting Checkbox Click to Confirm the Change

余生颓废 提交于 2019-12-11 07:40:05

问题


I have a checkbox and as soon as it is clicked I'm going to submit an AJAX request to update a field. However, first I'd like to call confirm to make sure that this is what they want.

This does not work:

$(".mycheckbox")
    .live("click",
        function(){
            if(!confirm("Sure about that?")){ return false; }
            $.post($(this.form).attr("action")+".js",
                   $(this).serialize()+"&_method=put",
                   null, 
                   "script");
        }
    )

回答1:


instead of return false:

function(ev){
    if(!confirm("Sure about that?")){
        ev.preventDefault();
        return;
    }
    ...
}



回答2:


You just have to wrap your confirm around the post:

$(".mycheckbox")
    .live("click",
        function(){
            if( confirm("Sure about that?")){ 
                  $.post($(this.form).attr("action")+".js",
                   $(this).serialize()+"&_method=put",
                   null, 
                   "script");
             }
        }
    )


来源:https://stackoverflow.com/questions/5944889/intercepting-checkbox-click-to-confirm-the-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!