防止表单提交的jQuery不起作用 - asp经典

时间:2018-03-01 05:13:44

标签: javascript jquery ajax

我在提交时调用一个函数来检查是否已完成所需的上传,如果数据库中没有上传,则不应提交表单。

通过使用alert,我可以看到我的checkuploads.asp返回了正确的值,它显示的是没有找到数据但没有阻止表单提交的提示。

$(window).load(function() {
    $('#Save').click(function() {
        $.ajax({
            type: "POST",
            url: "checkuploads.asp?value=" + $('#RequestNo').val(),
            success: function(data) {
                if (data === "Not Found") {
                    alert("the data was not found");
                    data.preventDefault();
                    window.history.back();
                    return false;
                } else
                if (data === "Found") {
                    alert("the data was found");
                    alert(data);
                }
            }
        });
    });
});

提前致谢。

2 个答案:

答案 0 :(得分:0)

由于您使用的是ajax请求,因此在提交表单之前,回复将需要一段时间。因此,在调用ajax后需要return false;

$(window).load(function() {
    $('#Save').click(function() {
        $.ajax({
            type: "POST",
            url: "checkuploads.asp?value=" + $('#RequestNo').val(),
            success: function(data) {
                if (data === "Not Found") {
                    alert("the data was not found");
                    //data.preventDefault();//Remove this un-necessary line
                    window.history.back();
                    //return false;//Remove this too. It is un-necessary
                } else
                if (data === "Found") {
                    alert("the data was found");
                    alert(data);
                    //Replace the form id with correct one
                    ("#myForm").submit();//Add this line
                }
            }
        });
        return false;
    });
});

以下是完整的示例:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <meta charset="UTF-8">
        <script src="jquery-3.2.1.min.js" type="text/javascript"></script>
    </head>
    <body>
        <div>
            <form id="myForm" action="/my-listener">
                <input type="text" name="RequestNo" id="RequestNo" />
                <input type="button" id="Save" value="Save" />
            </form>
        </div>
        <script>
            $(document).ready(function() {
                $('#Save').click(function() {
                    $.ajax({
                        type: "POST",
                        url: "checkuploads.asp?value=" + $('#RequestNo').val(),
                        success: function(data) {
                            if (data === "Not Found") {
                                alert("the data was not found");
                                window.history.back();
                            } else if (data === "Found") {
                                alert("the data was found");
                                alert(data);
                                $("#myForm").submit();
                            }
                        }
                    });
                    return false;
                });
            });
        </script>
    </body>
</html>

答案 1 :(得分:0)

试试这个:

         $(window).load(function() {
                $('#Save').click(function(e) {
                e.preventDefault();
                    $.ajax({
                        type: "POST",
                        url: "checkuploads.asp?value=" + $('#RequestNo').val(),
                        success: function(data) {
                            if (data === "Not Found") {
                                alert("the data was not found");
                                data.preventDefault();
                                window.history.back();
                                return false;
                            } else
                            if (data === "Found") {
                                alert("the data was found");
    window.location.replace( "checkuploads.asp?value=" + $('#RequestNo').val()); // you can chanbge url as per your need
// in case of form use
$('#form').submit();
                                alert(data);
                            }
                        }
                    });
                    return false;
                });
            });