jQuery Validate with Summernote Editor 错误:无法读取未定义的属性“替换" [英] jQuery Validate with Summernote Editor error: Cannot read property 'replace' of undefined
问题描述
我正在使用 MVC5 构建一个带有 Summernote 编辑器的表单.
剃刀代码:
@Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label" })@Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control post-content"} })@Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
JS:
$('#blog-form .post-content').summernote({高度:400,最小高度:300,代码镜像:{主题:'默认'}});
通过上述设置,编辑器控件呈现良好.但是,一旦我离开编辑器,即 onBlur,我就会在控制台中收到以下错误:
未捕获的类型错误:无法读取未定义的属性替换"在 $.validator.escapeCssMeta (jquery.validate.js:1014)在 $.validator.errorsFor (jquery.validate.js:995)在 $.validator.prepareElement (jquery.validate.js:679)在 $.validator.element (jquery.validate.js:466)在 $.validator.onfocusout (jquery.validate.js:289)在 HTMLDivElement.delegate (jquery.validate.js:411)在 HTMLFormElement.dispatch (jquery-2.2.3.js:4737)在 HTMLFormElement.elemData.handle (jquery-2.2.3.js:4549)在 Object.trigger (jquery-2.2.3.js:7819)在 Object.simulate (jquery-2.2.3.js:7890)
这是 DOM 的渲染部分:
<label class="control-label" for="Content">Content</label><textarea class="form-control post-content text-box multi-line" id="Content" name="Content" style="display: none;"></textarea><div class="note-editor note-frame panel panel-default">...夏季笔记生成...<span class="field-validation-valid text-danger" data-valmsg-for="Content" data-valmsg-replace="true"></span>
jQuery 版本:2.2.3
jQuery 验证版本:1.16.0
Microsoft.jQuery.Unobtrusive.Validation 版本:3.2.3
我做了一些研究,已经知道这与summernote插件无关.我尝试在准备好外部文档的内部和外部运行以下代码,但没有用:
$.validator.setDefaults({忽略: ":hidden:not(.post-content)"});
有什么想法吗?
我使用这个脚本告诉验证器忽略 Summernote 元素.可以调整它以忽略其他 HTML 编辑器生成的元素.
$('form').each(function () {如果 ($(this).data('validator'))$(this).data('validator').settings.ignore = ".note-editor *";});
I am using MVC5 to build a form with summernote editor.
Razor code:
<div class="form-group">
@Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control post-content"} })
@Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
</div>
JS:
$('#blog-form .post-content').summernote({
height: 400,
minHeight: 300,
codemirror: {
theme: 'default'
}
});
With the above setup the editor control renders fine. However, as soon as I move away from the editor, i.e. onBlur, I get the following error in the console:
Uncaught TypeError: Cannot read property 'replace' of undefined
at $.validator.escapeCssMeta (jquery.validate.js:1014)
at $.validator.errorsFor (jquery.validate.js:995)
at $.validator.prepareElement (jquery.validate.js:679)
at $.validator.element (jquery.validate.js:466)
at $.validator.onfocusout (jquery.validate.js:289)
at HTMLDivElement.delegate (jquery.validate.js:411)
at HTMLFormElement.dispatch (jquery-2.2.3.js:4737)
at HTMLFormElement.elemData.handle (jquery-2.2.3.js:4549)
at Object.trigger (jquery-2.2.3.js:7819)
at Object.simulate (jquery-2.2.3.js:7890)
Here is the rendered part of the DOM:
<div class="form-group">
<label class="control-label" for="Content">Content</label>
<textarea class="form-control post-content text-box multi-line" id="Content" name="Content" style="display: none;"></textarea>
<div class="note-editor note-frame panel panel-default">
...summernote generated...
</div>
<span class="field-validation-valid text-danger" data-valmsg-for="Content" data-valmsg-replace="true"></span>
</div>
jQuery version: 2.2.3
jQuery Validate version: 1.16.0
Microsoft.jQuery.Unobtrusive.Validation version: 3.2.3
I have done some research and already know that this is not related to summernote plug-in. I tried running the following code inside and of outside document ready, but it did not work:
$.validator.setDefaults({
ignore: ":hidden:not(.post-content)"
});
Any ideas?
I use this script to tell the validator to ignore Summernote elements. It can be tweaked to ignore elements generated by other HTML editors.
$('form').each(function () {
if ($(this).data('validator'))
$(this).data('validator').settings.ignore = ".note-editor *";
});
这篇关于jQuery Validate with Summernote Editor 错误:无法读取未定义的属性“替换"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!