我们为 TinyMCE 开发了一个内部开发的文件/图像/文档管理器插件,该插件仍在移植到 jQuery。同时,我们的一些依赖这些特性的项目需要使用基于原型的 TinyMCE 和 jQuery.noConflict() 版本。这很好用,但是在 ASP.NET MVC 3 中使用不显眼的验证,提交验证发生在触发 TinyMCE 回调以将 TinyMCE 的内容复制到表单字段之前。是否可以在不显眼的验证中挂钩预验证事件?
问问题
3934 次
2 回答
14
如果您使用提交按钮发布表单,请尝试以下操作:
$("input[type='submit']").click(function () {
tinyMCE.triggerSave();
});
如果您不使用提交按钮,只需在表单提交之前立即挂钩任何事件并调用 tinyMCE.triggerSave()。
于 2011-02-06T07:04:30.763 回答
3
另一种为您提供更多控制权的方法是在 TinyMCE 初始化中。这很好用,除了一种情况:您退出的最后一个 TinyMCE 实例不会触发 TinyMCE 中的 onDeactivate 事件(它仅在您转到另一个 TinyMCE 实例时触发)。因此,这是一种解决此问题的方法-到目前为止,它似乎运行良好,但是 YMMV。
注意:我会将此与接受的答案结合使用。此代码在 TinyMCE 中编辑内容时触发验证。
tinyMCE.init({
...
setup: ValidationTinyMceSetup
});
而我们的设置方法:
function ValidationTinyMceSetup(editor) {
var $textarea = $('#' + editor.editorId);
// method to use to save editor contents to backend input field (TinyMCE hides real input and syncs it up
// with values on form submit) -- we need to sync up the hidden input fields and call the valid()
// method from jQuery unobtrusive validation if it is present
function save(editor) {
if (editor.isDirty) {
editor.save();
var $input = $('#' + editor.editorId);
if (typeof $input.valid === 'function')
$input.valid();
}
}
// Save tinyMCE contents to input field on key/up down (efficiently so IE-old friendly)
var typingTimerDown, typingTimerUp;
var triggerDownSaveInterval = 1000; // time in ms
var triggerUpSaveInterval = 500; // time in ms
editor.onKeyDown.add(function (editor) {
clearTimeout(typingTimerDown);
typingTimerDown = setTimeout(function () { save(editor) }, triggerDownSaveInterval);
});
editor.onKeyUp.add(function () {
clearTimeout(typingTimerUp);
typingTimerUp = setTimeout(function () { save(editor) }, triggerUpSaveInterval);
});
// Save tinyMCE contents to input field on deactivate (when focus leaves editor)
// this is via TAB
editor.onKeyDown.add(function (editor, event) {
if (event.keyCode === 9)
save(editor);
});
// this is when focus goes from one editor to another (however the last one never
// triggers -- need to enter another TinyMCE for event to trigger!)
editor.onDeactivate.add(function (editor) {
save(editor);
});
}
最后,一个完全不相关的奖励项目:您可以通过在 JavaScript 源代码中包含此函数来添加字符计数器:
function CharacterCountDisplay(current, max) {
if (current <= max) {
return current + ' of ' + max + ' characters max';
}
else {
return '<span style="color: red;">' + current + '</span> of ' + max + ' characters';
}
}
并在上述ValidationTinyMceSetup
方法中添加:
// check for character count data-val
var character_max = $textarea.attr('data-val-lengthignoretags-max');
if (typeof character_max === 'undefined' || character_max === false) {
character_max = $textarea.attr('data-val-length-max');
}
if (typeof character_max !== 'undefined' && character_max !== false) {
var character_count = function (editor) {
var currentLength = $(editor.getDoc().body).text().length;
editor.dom.setHTML(tinymce.DOM.get(editor.id + '_path_row'), CharacterCountDisplay(currentLength, character_max));
};
// on load show character count
editor.onInit.add(character_count);
// while typing update character count
editor.onKeyUp.add(character_count);
}
要使用,只需将一个添加data-val-length-max="250"
到您的 textarea 标签或您正在使用 TinyMCE 的任何东西!
于 2011-12-15T17:35:49.877 回答