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

问题描述 投票:0回答:2

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

通过使用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);
                }
            }
        });
    });
});

提前致谢。

javascript jquery ajax
2个回答
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>

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;
                });
            });
© www.soinside.com 2019 - 2024. All rights reserved.