2010-09-08 27 views
0

當用戶嘗試提交表單而未在必填字段(class =「required」)中輸入數據時,字段將突出顯示並給出焦點。除非該字段是選擇選項。該字段突出顯示,但用戶不會返回到該字段。用戶必須在這種情況下向上滾動。Jquery表單驗證,將用戶返回到必填字段

我需要窗體滾動到突出顯示的字段,因爲它與其他輸入類型一樣。有誰知道這個解決方案?

TIA。

回答

0

你有沒有試過類似的東西?它將我滾動到選擇位於Firefox/Safari/Chrome的頂部。

$('#form').submit(function(e) { 
    if (yourSelectFailsValidaton) { 
     e.preventDefault(); 
     $('select').focus(); 
    } 
}); 

http://jsfiddle.net/9YwDF/

0

我將與錨標記做。爲每個需要的輸入創建一個錨點。檢查所需的輸入。如果您發現一個空白的,跳轉到相應的錨點。

<html> 
    <head> 
    <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
    <script type="text/javascript"> 
     google.load("jquery", "1"); 
    </script> 
    <script type="text/javascript"> 
     function jumpToRequired() { 
     $("input.required").each(function() { 
      var input = $(this); 
      if (!input.attr("value")) { 
      window.location.hash = input.attr("id") + "_a"; 
      return; 
      } 
     }); 
     } 
    </script> 
    </head> 
    <body> 
    <a name="whatever_a"></a> 
    <input type="text" id="whatever" class="required" /> 
    <button type="button" onclick="jumpToRequired()">Jump to required.</button> 
    </body> 
</html>