2013-10-28 47 views
0

單擊按鈕時; 如果所有文本框不是空的在它將指向下一頁的頁面上。我做了控制它的工作。但我怎樣才能定位到另一個頁面與jQuery?頁面方向與JQuery的?

$(document).on("pageinit", "#registerPage1", function() { 
    $(".nextBtn").click(function() { 
     if ($(".txt").val().lenght != 0) { 
      // i want to write codes for orientation registerPage1 to registerPage2 in here 
     } 

     $(".txt").each(function() { 
      if ($(this).val().length == 0 && $(this).next().attr('class') != 'nullInputMsg') { 
       ($(this)).after('<div class="nullInputMsg">this field is required!</div>'); 
      } 
      else if ($(this).val().length != 0 && $(this).next().attr('class') == 'nullInputMsg') 
       $(this).next().remove(); 
     }); 
    }); 

}); 

回答

0

讓我們假設你有一個名爲myform的表格,它包含你所有的文本框。讓我們也假定類nextBtn的按鈕位於此表單內,並觸發表單的提交行爲。

就像你所做的那樣,驗證提交按鈕的click事件上的表單是沒問題的。但是,只有在所有驗證都通過的情況下,纔會移動到下一個頁面,因此,您應該離開重定向部分直到最後,到時候你會確定驗證檢查的結果。在此之後,所有剩下要做的就是

  1. 一套「myform` action屬性指向所需的頁面。(這redirectes此頁)
  2. 返回false如果驗證失敗,或真如果他們從處理點擊事件的函數傳遞過來的話。

所以,你的代碼將看起來像

$(document).on("pageinit", "#registerPage1", function() { 
      $(".nextBtn").click(function() { 
       var validationPass = true; 

       $(".txt").each(function() { 
        if ($(this).val().length == 0 && $(this).next().attr('class') != 'nullInputMsg') { 
         ($(this)).after('<div class="nullInputMsg">this field is required!</div>'); 
         validationPass = false; 
        } 
        else if ($(this).val().length != 0 && $(this).next().attr('class') == 'nullInputMsg') 
         $(this).next().remove(); 
       }); 

       return validationPass; 
      }); 

     }); 

你的HTML或許應該像

 .... 
    .... 
     <form id="myform" name="myform" action="RedirectToPage.php" method="get"> 
     .... 
     //form content housing the textboxes and button with class .nextBtn 
     .... 
     </form> 
    .... 
    .... 
+0

是的,我做了類似的想法: $ x =「true」; $(「。requiredText」)。each(function(i){if(this).val()。length) $ x =「false」; } ); if($ x ==「true」) // commands }); });; }); – ebruszl

0

我想你的意思是你的意思是重定向。您不需要jQuery進行重定向。簡單的JavaScript將完成這項工作。

// works like the user clicks on a link 
window.location.href = "http://google.com"; 

// works like the user receives an HTTP redirect 
window.location.replace("http://google.com"); 
+0

謝謝,我想這$(windows.location).attr( '身份證' ,'registerPage2');但我沒有工作。這是工作window.location.href =#registerPage2「;我不能寫這與jquery? – ebruszl

+0

有可能是你可以但你爲什麼?這是一個乾淨的解決方案:) – sebbo