[英]jquery submit form after check validation
如果用户使用jquery选中了没有更改URL的复选框,则需要将用户从索引页面重定向到另一个名为“ free.php”的页面。 我在这里创建了一个示例,
这里的复选框正在验证,但是我无法重定向到free.php。虽然我是jquery和ajax的新手,但找不到答案。 我需要在代码中进行哪些更改?
谢谢!
除非您在其中一个处理程序中进行物理重定向,否则无法使用$.ajax
调用进行重定向。 但是,这将破坏您不更改浏览器URL的意图。
不更改浏览器URL的唯一方法是:
iframe
并将其来源更改为free.php
处理ajax
并将来自free.php
HTML
响应注入到DOM
:
$.ajax({
type: "POST",
url: "free.php",
data: formdata,
dataType : 'html',
success : function(html) { $('div.result').html(html); }
});
为什么不将操作设置为free.php并删除ajax调用? 因此它将是:
$("#agreeForm").submit(function()
{
if (!$("#agree").is(':checked'))
{
document.getElementById("err").innerHTML="you must agree to terms if you would like to access web";
return false;
}
});
我不认为$.ajax
是您根据描述所做的工作,$。ajax()用于调用页面并从中获取响应而无需重新加载。
我将一起删除ajax调用,并将您的表单标题更改为<form method="POST" action="free.php" id="agreeForm" name="Agree">
$(document).ready(function() {
$("#agreeForm").submit(function()
{
if (!$("#agree").is(':checked'))
{
document.getElementById("err").innerHTML="you must agree to terms if you would like to access web";
return false;
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.