Jquery提交表单然后重定向不工作
Jquery submit form then redirect not working
我已经找到了很多关于这个的帖子,但是我不能让这个为我的生活工作。在提交表单时,我需要代码来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中的转换作为函数的一部分。
表单提交和页面重定向是冲突的,因为在一个基本的网页中,只有一个可以发生。当表单提交时,它将通过所需的方法将表单数据发送到服务器-通常是GET
或POST
。
相反,当重定向开关语句运行时,它将告诉浏览器更改页面url。
这两个事件都将触发浏览器内的页面更改,但只有一个事件会真正发生。
最有效的方法是通过AJAX请求提交表单数据,然后在完成时调用重定向代码。
看起来你已经在使用jQuery了,看看这里的post方法
相关文章:
- 通过链接重定向不;我不在jstree中工作
- 意思是.js - 登录后重定向用户 - 在本地工作,但在部署时不起作用
- JavaScript没有'JSP页面重定向后无法工作..任何解决方案
- 带有SetTimout的Javascript重定向不工作
- window.location重定向没有'我不在IE8上工作
- 基于变量的Javascript URL重定向无法正常工作
- 为什么JavaScript URL表单无法工作,也无法重定向
- JavaScript警报重定向无法正常工作
- co重定向无法正常工作
- 角度重定向到其他方式而不检测视图,但当我单击后退按钮时它可以工作
- 轮播在初始重定向时中断,但在刷新时工作正常
- JavaScript重定向它不是在服务器上工作,而是在本地主机上工作
- HTML POST->重定向工作,XMLHTTPrequest->重定向不起作用(无法从null发出任何请求
- Javascript重定向:symfony路由中的脚本不工作
- 如果语句重定向未按预期工作
- 使提交表单在不重定向或刷新的情况下工作
- 确认/重定向功能不工作-javascript
- 重定向未按预期工作
- JS重定向未按预期工作
- Onclick重定向到下一页不工作在phonegap