我正在使用require_from_group進行驗證,但它不起作用。jQuery驗證require_from_group不工作
我的腳本代碼是在這裏:
jQuery.validator.setDefaults({ 調試:真實, $(文件)。就緒(函數(){
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$("#findproject_form").validate({
rules: {
Profession: {
require_from_group: [1, ".validategroup"]
},
Location1: {
require_from_group: [1, ".validategroup"]
},
Company: {
require_from_group: [1, ".validategroup"]
},
Name: {
require_from_group: [1, ".validategroup"]
},
KeyWord: {
require_from_group: [1, ".validategroup"]
}
}
});
$.validator.addMethod("require_from_group", function (value, element, options) {
var $fields = $(options[1], element.form),
$fieldsFirst = $fields.eq(0),
validator = $fieldsFirst.data("valid_req_grp") ? $fieldsFirst.data("valid_req_grp") : $.extend({}, this),
isValid = $fields.filter(function() {
return validator.elementValue(this);
}).length >= options[0];
// Store the cloned validator for future validation
$fieldsFirst.data("valid_req_grp", validator);
// If element isn't being validated, run each require_from_group field's validation rules
if (!$(element).data("being_validated")) {
$fields.data("being_validated", true);
$fields.each(function() {
validator.element(this);
});
$fields.data("being_validated", false);
}
return isValid;
}, $.validator.format("Please fill at least {0} of these fields."));
});
當任何一個或多個字段爲fill_out時,它會給出錯誤,而不是當它爲空並且沒有到達動作頁面時 那麼這有什麼問題
我的形式IS HERE
<form id="findproject_form" method="post" action="{$BASE_URL}findproject" >
<div class="bgtrans"><h3>Search By</h3>
<div class="div_bg1">
<div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Profession" id="Profession" name="Profession" value="{if $profession}{$profession|escape}{/if}" /></div></div>
<div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Location" id="Location1" name="Location1" value="{if $location}{$location|escape}{/if}" /></div></div>
<div class="searchbg" style="margin-right:0px;">
<div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Company" id="Company" name="Company" value="{if $company}{$company|escape}{/if}"/></div>
</div>
<div class="clear"></div>
</div>
<div class="div_bg2">
<div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Name" id="Name" name="Name" value="{if $name}{$name|escape}{/if}" /></div></div>
<div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Key Words" id="KeyWord" name="KeyWord" value="{if $keyword}{$keyword|escape}{/if}"/></div></div>
<div class="searchbg" style="margin-right:0px;">
<input class="bgbttn" type="submit" name="submit" value="" />
</div>
<div class="clear"></div>
</div>
</div>
你不需要試圖驗證反對前增加你的方法是什麼? – rjdown 2014-12-03 12:21:43
如果我刪除它給出錯誤:檢查元素Location1時發生異常,請檢查'require_from_group'方法 – 2014-12-03 12:22:59
這不是** require_from_group函數... [這只是'addMethod'文檔中的示例代碼](http ://jqueryvalidation.org/jQuery.validator.addMethod/)。 – Sparky 2014-12-03 23:49:42