0
我使用jQuery與Validate Plugin和Nathan Long的真棒方法require_from_group不幸的是我遇到了驗證表單內其他字段的問題。問題與jQuery驗證插件不驗證前半部分
任何超出所需字段集的地方都沒有正確顯示錯誤信息。
這(加的jQuery 1.4.2和驗證插件)是我的代碼:
<form>
<label for="fullName">Name <em>required</em></label>
<input id="fullName" name="fullName" type="text" class="required"><br />
<fieldset>
<legend>Enter 1 of 3 emails <em> required</em></legend>
<label for="field1">field1</label><input id="field1" class="group_required email" type="text" name="field1" /><br />
<label for="field2">field2</label><input id="field2" class="group_required email" type="text" name="field2" /><br />
<label for="field3">field3</label><input id="field3" class="group_required email" type="text" name="field3" /><br />
</fieldset>
<input name="submitbutton" type="submit" />
</form>
<script>
jQuery.validator.addMethod("require_from_group", function(value, element, options) {
numberRequired = options[0];
selector = options[1];
//Look for our selector within the parent form
var validOrNot = $(selector, element.form).filter(function() {
// Each field is kept if it has a value
return $(this).val();
// Set to true if there are enough, else to false
}).length >= numberRequired;
if(!$(element).data('being_validated')) {
var fields = $(selector, element.form);
//.valid() means "validate using all applicable rules" (which
//includes this one)
fields.data('being_validated', true).valid();
fields.data('being_validated', false);
}
return validOrNot;
// {0} below is the 0th item in the options field
}, jQuery.format("Please fill out at least {0} of these fields."));
$(document).ready(function(){
$("form").validate({
debug: true,
});
jQuery.validator.addClassRules("group_required", {require_from_group: [1, ".group_required"]});
});
</script>
實際的頁面比較複雜,但是這是你需要複製該問題的代碼。提交表單而不填寫任何字段,您將在組中的3旁邊看到一條錯誤消息,但不會顯示名稱字段。如果名稱字段在HTML上的字段集驗證後工作正常。賦予名稱字段焦點然後模糊確實會生成正確的錯誤消息,但不會在提交時顯示。
我看到另一個人的,在對原有問題的意見這個問題,但沒有看到任何回覆:(
註釋掉:
fields.data('being_validated', true).valid();
修復該錯誤消息顯示問題,但重-introduces的問題,森正試圖解決的問題。
'class =「required''足以讓fullName字段根據需要進行驗證。它可以工作,如果我點擊進出現場。 – 2011-03-16 18:47:07