如果放在confirm()和AJAX之前,.html()不会更改内容

时间:2018-01-19 02:10:50

标签: javascript jquery ajax

我想使用.html()更改元素的内容,以指示进程已启动。

但问题是,无论我做什么,它都行不通。我可能会遗漏一些阻止脚本工作的东西。

代码:

if(counter < 1 && flag == true){
                                alert("test");
                                $("#updownform").html("Please Wait...");
//                                if(confirm("Are you sure you want to proceed?")){
//                                    counter++;
//                                    $.ajax({
//                                        method: "POST",
//                                        url: "index.php?module=Accounts&action=processUpdowngrade",
//                                        data: {form_data: $(this).serialize()},
//                                        success: function (datas) {
//                                            counter = 10000;
//                                            location.reload();
//                                        }
//                                    });
//                                }else{
//                                    $("#updownform").html("Save changes");
//                                }
                            }

在此示例中,除了alert和.html()之外,所有内容都被注释掉了。 .html()有效但如果我取消注释所有内容,只有在AJAX完成请求后,即使在触发确认条件后,它也会起作用。我发现这很奇怪,因为我已经在确认条件之前放置了它。理想情况下,它会在确认条件之前执行。

我也尝试过使用beforeSend,但它仍然没有用。我也尝试将它设置为asynch:false / true和beforeSend无效。

1 个答案:

答案 0 :(得分:0)

我建议您尝试下面的代码,看看它是否有帮助。

<script>
if (counter < 1 && flag == true) {
    alert("test");
    document.getElementById("updownform").value = "Please Wait...";
    if (confirm("Are you sure you want to proceed?")) {
        counter++;
        $.ajax({
            type: "POST",
            url: "index.php?module=Accounts&action=processUpdowngrade",
            data: $("#enter_your_form_id").serialize(), // serializes the form's elements.
            success: function(data) {
                counter = 10000;
                location.reload();
            }
        });
    } else {
        document.getElementById("updownform").value = "Save changes";
    }
}
</script>