问题描述
我正在尝试在tinymce编辑器中添加一个验证。条件是没有给出所有强制输入的用户不能提交表单。没关系,直到我在tinymce中添加了表格插件。在编辑器中添加表后,当我在该表上输入一些内容时,我的代码无法从该表中读取该值,但是如果我在表外提供该值,则验证工作正常。 据我了解,由于某种原因,编辑器无法获取表标签值。这是我的代码:
var moduleTypesMap = {};
$.each(moduleTypesJson,function (index,item){
moduleTypesMap[item.name] = item;
moduleTypesMap[item.name]['placeholders'] = item.placeholders.split(',');
});
tinymce.init({
selector: '#textarea',plugins: 'table',toolbar: 'table tabledelete | tableprops tablerowprops tablecellprops | tableinsertrowbefore tableinsertrowafter tabledeleterow | tableinsertcolbefore tableinsertcolafter tabledeletecol'
});
$submitButton.on('click',function (){
var body = tinymce.get('textarea').getContent(); //gets the whole content of the table
var isBodyValid = true;
var type = $template.val(); //my dropdown values
$.each(moduleTypesMap[type].placeholders,item){
if (body.indexOf(item) === -1)
isBodyValid=false;
});
if(isBodyValid){
$form.trigger('submit');
} else {
bootBox.alert("please give all placeholders");
}
});
我在编辑器中提供值的方式是:
{name},{roll},{school}。
但是,如果我在表上给出值{name},那么它将无法读取该值。那个时间项{name}得到索引(body.indexOf(item) === -1)
得到-1,而表外它的值大于1。我不知道为什么表值没有得到大于1的索引号。
[NB] 已解决:
实际上,我正在传输的输入中存在一些不匹配,这就是为什么不起作用的原因。但现在解决了。
解决方法
我不认为问题与tinyMCE有关。请检查下面的代码段。
https://codepen.io/zortext/pen/abNxePB
tinymce.init({
selector: '#textarea',plugins: 'table',toolbar: 'table tabledelete | tableprops tablerowprops tablecellprops | tableinsertrowbefore tableinsertrowafter tabledeleterow | tableinsertcolbefore tableinsertcolafter tabledeletecol'
});
function getContentIndex(str){
alert(tinymce.get('textarea').getContent().indexOf(str))
}
成功警告索引