问题
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