为了进行验证,我将插件JQuery Validation(jquery.validate.js)与Additional-methods.js结合使用。该表单包含许多输入,在其中检查要填写的字段。我对文件使用onkeyup规则。一切正常,但对tinymce的onkeyup规则无效。什么需要注册才能生效?
jQuery(function ($) {
var validator = $("#adminForm").submit(function() {
// update underlying textarea before submit validation
tinyMCE.triggerSave();
}).validate({
onkeyup: function(element) {
this.element(element); // <- "eager validation"
},
onfocusout: function(element) {
this.element(element); // <- "eager validation"
},
ignore: "",
rules: {
title: {
required: true,
minlength: 10,
maxlength: 50
},
donate: {
required: true,
digits: true,
min: 1,
max: 60000
},
text: {
required: true,
minlength: 250
},
checkbox: {
required: true
}
},
messages: {
title: {
required: "enter the title",
minlength: "no less than 10 symbols",
maxlength: "no more than 50 symbols"
},
donate: {
required: "enter the number",
digits: "Not a number",
min: "no less than 1",
max: "no more than 60000"
},
text: {
required: "here is your text",
minlength: "no less than 250 symbols"
},
checkbox: {
required: "do you public spam?"
}
},
errorPlacement: function(label, element) {
// position error label after generated textarea
if (element.is("textarea")) {
label.insertAfter(element.next());
} else {
label.insertAfter(element)
}
}
});
validator.focusInvalid = function() {
// put focus on tinymce on submit validation
if (this.settings.focusInvalid) {
try {
var toFocus = $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []);
if (toFocus.is("textarea")) {
tinyMCE.get(toFocus.attr("id")).focus();
} else {
toFocus.filter(":visible").focus();
}
} catch (e) {
// ignore IE throwing errors when focusing hidden elements
}
}
}
});
加载TinyMCE时,编辑器区域实际上是iframe
而不是主页的一部分,因此keyup
之类的事件不会在主页中发生,而是在iframe
中发生。您当然也可以在iframe
中添加事件处理,但这就是为什么您看不到基于在编辑器中键入而触发这些事件的原因。