2013-10-28 43 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