문제

I'm trying to do a simple: if (confirm()) { actions }, but some actions are done even if you didn't press confirm / cancel yet.

Here's my code:

$("#checkbox").click(function() {

    if (confirm("Continu ?")) {
        hideShowDiv("test");
        $(this).attr("checked", true);
    }
}

I this case, if you click on the checkbox, even if you didn't press confirm / cancel, the checkbox will be checked, but the div 'test' won't be displayed (unless you press confirm). I don't know why the line:

 $(this).attr("checked", true);

is executed before the return of confirm().

도움이 되었습니까?

해결책

try add e.preventDefault();

$("#checkbox").click(function(e) {
    e.preventDefault();
    if (confirm("Continu ?")) {
        hideShowDiv("test");
        $(this).attr("checked", true);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top