您好,我正在为我的自定义文本字段之一使用 Drupal 的 6 个默认 jQuery 自动完成功能。
现在我想添加 spellchecker 以及 plural check。
例如
Word: Potatos
Spellchecker suggestions (including plural check): Potato, Potatoes
有什么方法可以实现此功能,以便在用户键入 Potatos 时。首先会运行拼写检查脚本,它会建议正确的备选词,一旦用户选择了正确的单词,自动完成脚本就会运行?
最佳答案
您需要做的是首先安装服务器端解决方案来执行拼写检查,例如您可以使用 PSpell .如果您无法安装 PSpell,您可以使用 Googlespell(一定要查看使用条款)还有一些客户端实现可用(尽管不如服务器端解决方案可靠)。
有很多插件可以帮助您进行拼写检查,但对于您的任务,我会选择更轻量级的插件,例如 jQuery spellchecker。 .
使用所需的选项启动拼写检查器,并为文本字段创建一个事件监听器,一个简单的示例是:
var spellchecker = new $.SpellChecker('textarea', {
lang: 'en',
parser: 'text',
webservice: {
path: '../../webservices/php/SpellChecker.php',
driver: 'pspell'
},
suggestBox: {
position: 'above'
}
});
$('textarea').keyup(function() {
//you would probably want to add a timer looking to see if user is finished typing
spellchecker.check();
});
然后你会想要为拼写检查器事件replace.word
添加一个监听器:
spellchecker.on('replace.word', function() {
//ie: submit the form
});
如果你想做一些更高级的事情,你也可以尝试覆盖自动完成 Drupal.jsAC
事件 .keyup()
中内置的 Drupals 并查看事件 check.success
被触发(没有拼写错误的单词)。如果没有拼写错误的单词,您可以对 onkeyup 事件使用 drupals 自动完成实现。如果有拼写错误的单词,则对这些事件不执行任何操作。
var misspelledWord = false;
spellchecker.on('check.success', function() { misspelledWord = false; });
spellchecker.on('check.fail', function() { misspelledWord = true; });
jQuery(document).ready(function(){
Drupal.jsAC.prototype.onkeyup = function (input, e) {
if(misspelledWord) {
if (!e) {
e = window.event;
}
switch (e.keyCode) {
case 16: // shift
case 17: // ctrl
case 18: // alt
case 20: // caps lock
case 33: // page up
case 34: // page down
case 35: // end
case 36: // home
case 37: // left arrow
case 38: // up arrow
case 39: // right arrow
case 40: // down arrow
return true;
case 9: // tab
case 13: // enter
case 27: // esc
this.hidePopup(e.keyCode);
return true;
default: // all other keys
if (input.value.length > 0)
this.populatePopup();
else
this.hidePopup(e.keyCode);
return true;
}
}
};
});
但是对我来说,通过在 spellcheckers replace.word 事件中使用 jQuery 的 AJAX 功能来实现您自己的自动完成功能更有意义 - 您也可以使用/自定义其中一个 jQuery 自动完成插件。
关于php - 自动完成以及文本字段的拼写检查和复数检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12363615/