2010-05-05 58 views
3

我的表單是通過使用JavaScript的鏈接提交的,但我也試圖驗證從justing jQuery驗證。鏈接提交時驗證不起作用,但如果將鏈接更改爲提交按鈕,驗證不起作用。我究竟做錯了什麼?由JavaScript提交的表單上的jQuery驗證

我的形式:

<form id="findmatch" method="post" action="search"> 
    <div> 
     <label class="formlabel">Match Type 
      <input type="text" name="matchtype" id="matchtype" class="forminput" /> 
     </label> 

     <label class="formlabel">Location (postcode) 
      <input type="text" name="location" id="location" class="forminput" /> 
     </label> 

     <label class="formlabel">Radius (miles) 
      <input type="text" name="Radius" id="Radius" class="forminput" /> 
     </label> 

     <label class="formlabel">Keywords 
      <input type="text" onblur="javascript:usePointFromPostcode(document.getElementById('location').value, showCompleteLatLng)" onchange="javascript:usePointFromPostcode(document.getElementById('location').value, showCompleteLatLng)" name="keywords" id="keywords" class="forminput" /> 
     </label> 

     <input id="lat" class="hidden" name="lat" type="text" value="" /> 
     <input id="lon" class="hidden" name="lon" type="text" value="" /> 

     <a href="javascript:document.getElementById('findmatch').submit();" onmouseover="javascript:usePointFromPostcode(document.getElementById('location').value, showCompleteLatLng)" class="submit">Search</a>       
    </div> 
</form> 

我的jQuery是

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#findmatch").validate({ 
      rules: { 
       location: "required", 
       Radius: { 
        required: true, 
        digits: true 
       }, 
       keywords: "required" 
      }, 
      messages: { 
       location: "Please enter your postcode", 
       Radius: { 
        required: "Please enter a radius", 
        digits: "Please only enter numbers" 
       }, 
       keywords: "Please enter the keywords you wish to search for" 
      } 
     }); 
    }); 
</script> 
+0

'jQuery.valid ate()'有一個你可以設置的提交處理程序。也許這可能有幫助。 http://docs.jquery.com/Plugins/Validation/validate – dnagirl 2010-05-05 13:54:11

+0

爲什麼你使用提交按鈕的鏈接呢?您應該使用它們打算使用的HTML元素。 – akamike 2010-05-05 13:58:07

+0

更新了演示! – 2010-05-05 14:24:23

回答

1

DEMO:http://jsbin.com/urole/3

$(function() { 
    $('#submit').click(function(e) { 
    e.preventDefault(); 
    $("#commentForm").submit(); 
    }); 
    $("#commentForm").validate(); 
}); 

提交

+0

謝謝,這幫助了很多 – Daniel 2010-05-05 18:39:42

+0

你如何用Submit按鈕做到這一點?我嘗試使用提交按鈕,這不起作用。 – yogsma 2010-07-23 19:12:12