你需要做的是先安裝一個服務器端解決方案來執行你的拼寫檢查,你可以使用例如PSpell。如果您無法安裝PSpell,您可以使用Googlespell(只要確保查看使用條款)。還有一些客戶端實現可用(儘管不如服務器端解決方案可靠)。
這裏有很多插件可以幫助你進行拼寫檢查,但是對於你的任務ID,你可以選擇更爲輕量級的東西,比如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
});
如果你想要做一些更高級的,你也可以嘗試重寫Drupals內置自動完成Drupal.jsAC
事件.keyup()
,看看事件check.success
被解僱(N o拼寫錯誤的單詞)。如果沒有拼寫錯誤的單詞,您可以使用dukeals自動完成實現onkeyup事件。如果拼寫錯誤,對這些事件不做任何處理。
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;
}
}
};
});
但是它更有意義,我用jQuery's AJAX功能在拼寫檢查replace.word事件來實現自己的自動完成 - 你也可以使用/自定義的jQuery插件自動完成的一個。
cfk編輯器或tinymce想到 – 2012-09-11 06:28:08
@Dagon是否可以將相同的功能添加到文本字段? –