簡體   English   中英

jQuery的檢查確認后提交表格

[英]jquery submit form after check validation

如果用戶使用jquery選中了沒有更改URL的復選框,則需要將用戶從索引頁面重定向到另一個名為“ free.php”的頁面。 我在這里創建了一個示例,

http://jsfiddle.net/jKHQe/

這里的復選框正在驗證,但是我無法重定向到free.php。雖然我是jquery和ajax的新手,但找不到答案。 我需要在代碼中進行哪些更改?

謝謝!

除非您在其中一個處理程序中進行物理重定向,否則無法使用$.ajax調用進行重定向。 但是,這將破壞您不更改瀏覽器URL的意圖。

不更改瀏覽器URL的唯一方法是:

  • 合並iframe並將其來源更改為free.php
  • 處理ajax調用,並將響應注入到當前在頁面上的元素中。

處理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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM