2014-04-16 32 views
0

我正在使用Jquery驗證插件,並且它具有所需屬性的問題,因爲我的表單正在提交,儘管具有必需的屬性。屬性需要不工作在IE8/Safari使用Jquery驗證

我已經嘗試required =「required」,只是本身需要,但沒有運氣。

這裏是我的示例代碼:

<input id="txt_field1" name="txt_field1" type="text" required/> 

也試過:

<input id="txt_field2" name="txt_field2" type="text" required="required"/> 

我已經找到2種解決方法,但似乎並沒有是最好的方式。

$('form').validate({ 
     rules: { 
     txt_field1: { 
      required: true 
     }, 
     txt_field2: {  
      required: true 
     } 
     } 
}); 

這似乎已經工作,以及:

$('form').validate(); 
$('input[required]').each(function() { 

     $(this).rules("add", { 
      required: true 
     }); 

    }); 

有沒有不必添加額外的代碼來得到它的工作更好的辦法?我正在使用Jquery Validate 1.9版。

感謝

回答

-1

我認爲所需的屬性實際上是一個HTML5功能,而不是一個jQuery的功能。在jquery中,你不需要輸入一個必需的屬性,只需要根據需要聲明dom元素作爲你的第一個狀態周圍的工作。某些瀏覽器尚不支持所需的屬性。

對於Safari,表單驗證僅部分支持,即在提交期間未填寫所需表單時不會發出警告,而對於IE8則完全不支持。請點擊以下鏈接瞭解更多信息:http://caniuse.com/#feat=form-validation並在這裏http://www.w3schools.com/tags/att_input_required.asp

0

您必須設置class屬性要求。

<input id="txt_field2" name="txt_field2" type="text" class="required"/> 
+0

嗨,我試過了,它沒有工作。提交的表格並沒有顯示任何錯誤 –