包含的库
@Scripts.Render("~/Scripts/jquery-ui-1.11.4.min.js")
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
@Scripts.Render("~/Scripts/jquery.validate.js")
我的验证仅在点击按钮Add
时激活:
$('#form1').validate({
rules: {
PROGRAM: {
required: true,
maxlength:20
},
UNIT:"required",
IS_ACTIVE:"required"
},
messages: {
PROGRAM: {
required: "Program Code is required.",
maxlength: "Program code cannot be over 20 characters long"
},
UNIT: {
required: "Unit code is required. Please select one."
},
IS_ACTIVE:{
required:"Is it active?"
}
},
errorElement: 'div',
errorLabelContainer: 'validationErrDisplay'
});
$('#programUnitInquiry').validate().cancelSubmit = false;
以上代码以及事件处理程序(包括Add
按钮的代码)都包含在$(document).ready()
中。
在form1
中,PROGRAM
是一个文本框,其余是下拉列表。
当我单击UNIT
下拉列表时,会抛出运行时错误。错误来自/Scripts/jquery.validate.js
文件版本1.10.0中的第1234行:
if (!jQuery.event.special.focusin && !jQuery.event.special.focusout && document.addEventListener) {
$.each({
focus: 'focusin',
blur: 'focusout'
}, function( original, fix ){
$.event.special[fix] = {
setup:function() {
this.addEventListener( original, handler, true );
},
teardown:function() {
this.removeEventListener( original, handler, true );
},
handler: function(e) {
var args = arguments;
args[0] = $.event.fix(e);
args[0].type = fix;
return $.event.handle.apply(this, args);
}
};
function handler(e) {
e = $.event.fix(e);
e.type = fix;
return $.event.handle.call(this, e); // <-- error
}
});
}
错误讯息:
0x800a138f - JavaScript运行时错误:无法获取属性'call' 未定义或空引用
UNIT
下拉列表在呈现后的HTML:
<select name="UNIT" id="UNIT" data-val-required="The Unit Code field is required." data-val="true">
<option value="">-- Select a Unit --</option>
<!-- bunch of options retrieved from database -->
</select>