[英]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.