Jquery提交表单然后重定向不工作

Jquery submit form then redirect not working

本文关键字:工作 重定向 然后 提交 表单 Jquery      更新时间:2023-09-26

我已经找到了很多关于这个的帖子,但是我不能让这个为我的生活工作。在提交表单时,我需要代码来1)提交表单和2)重定向到case语句中的一个位置。我可以让表单在没有案例的情况下提交,也可以让表单重定向,但我无法同时做到这两点。请告诉我我是在正确的轨道上,只是代码在错误的地方,我已经盯着这个方向太久了,遇到了一个障碍。

$("#submitBtn").click(function (event) {
        event.preventDefault();
        var validator = $(this).closest("form").kendoValidator({
            messages: {
                required: function (input) { return getRequiredValidationMessage(input) },
                custom: function (input) { return getInvalidValidationMessage(input) },
            },
            rules: {
                custom: function (input) {
                    var minlength = $(input).attr('data-minlength');
                    var required = $(input).attr('required');
                    if (typeof minlength !== typeof undefined && minlength !== false && ((typeof required !== typeof undefined && required !== false) || $(input).val().length > 0)) {
                        var minlength = $(input).data('minlength');
                        return $(input).val().length >= minlength;
                    } else {
                        return true;
                    }
                }
            }
        }).data("kendoValidator");
        if (validator !== undefined) {
            if (validator.validate()) {
                $("aspnetForm").submit();
                if ($("aspnetForm").submit()){
                    switch(document.getElementById('interests').value){
                        case "stair-lifts":
                        window.location.href="/Catalog/Online-Catalog-Category/15522/Stair-Lifts";
                        break;
                        case "wheelchair-ramps":
                        window.location.href="/Catalog/Online-Catalog-Category/15521/Ramps";
                        break;
                        case "roll-in-barrier-free-showers":
                        window.location.href="/Catalog/Online-Catalog-Category/15529/Bathroom-Safety";
                        break;
                        case "walk-in-tubs":
                        window.location.href="/Catalog/Online-Catalog-Category/15529/Bathroom-Safety";
                        break;
                        case "patient-lifts-ceiling-lifts":
                        window.location.href="/Catalog/Online-Catalog-Category/15523/Patient-Lift";
                        break;
                        case "wheelchair-lifts":
                        window.location.href="/Catalog/Online-Catalog-Category/15525/Wheelchair--Scooter-Lifts";
                        break;
                        default:
                        window.location.href="/"; // if no selection matches then redirected to home page
                        break;
                    }// end of switch 
                }
            } else {
                $('.k-invalid:first').focus();
                $('.k-invalid').blur(function () { if (this.checkValidity()) { $('.k-invalid:first').focus(); } });
            }
        } else {
            $(this).closest("form").submit();
        }   
    });
});

您不应该使用.submit()。由于使用的是JQuery,因此应该使用AJAX请求,格式如下:

$("#submitBtn").click(function (event) {
    $.ajax({
        url : "/your/url", // Whatever URL you're submitting to goes here
        type : "post",
        data : yourData, // Whatever data you're sending goes here
        success : function(data) {
            // Whatever you want to do on success goes here
        }
    });
});

在AJAX查询成功执行时,将URL中的转换作为函数的一部分。

表单提交和页面重定向是冲突的,因为在一个基本的网页中,只有一个可以发生。当表单提交时,它将通过所需的方法将表单数据发送到服务器-通常是GETPOST

相反,当重定向开关语句运行时,它将告诉浏览器更改页面url。

这两个事件都将触发浏览器内的页面更改,但只有一个事件会真正发生。

最有效的方法是通过AJAX请求提交表单数据,然后在完成时调用重定向代码。

看起来你已经在使用jQuery了,看看这里的post方法