2011-11-06 79 views

回答

1

是的,你可以,但該領域仍然需要在一組<form>標籤。但是,您需要而不是「提交」此form以檢查內部字段的驗證。您可以使用.valid()方法獨立於form提交來檢查此表單。

http://jsfiddle.net/9fgVN/13/

<form id="myNonsubmitForm" action="#"> 
    <textarea name="comments" id="comments" rows="5" cols="30"></textarea> 
</form> 

<button id="myButton">Click to Check Validation Status</button> 
<input type="text" id="output" /> 

 

$(document).ready(function() { 

    $("#myNonsubmitForm").validate({ 
     validClass: 'valid', // as per your configuration 
     rules: { // set rules as per your configuration 
      comments: { 
       required: false, 
       maxlength: 10 
      } 
     }, 
     errorPlacement: function(error, element) { 
      // use this to control placement of error messages 
      // removal of errorPlacement handler will result in message appearing next to field automatically. 
     } 
    }); 

    $("#myButton").click(function() { // validate on button click for this example 
     if($("#myNonsubmitForm").valid()){ 
      $('#output').val('passed'); 
     } else { 
      $('#output').val('failed'); 
     }; 
    }); 

}); 
+0

@ ramb0tn1k,那麼,爲什麼你不能用'form'標籤? – Sparky

+0

你說得對。我仔細閱讀了文檔,得出結論:我無法使用表格的原因是沒有。謝謝! – ramb0tn1k