2012-12-09 58 views
1

我們使用gem'bootstrap-wysihtml5-rails','0.3.1.13'。我想在焦點丟失時從textarea保存已更改的內容。Rails&wysihtml5 - 更改後保存

我試圖jQuery的直接在視圖中使用,通過腳本標籤包圍:

$("textarea").live("blur", function(){ alert("Focus lost"); }); 

如果我使用「模糊」(或事件的內容)的警報在頁面加載多次觸發,而不是當失去焦點,當我使用「改變」時,什麼都沒有發生。

在另一個嘗試我試圖鉤到wysihtml5的事件與相同的行爲:

function integrate_wysihtml5() { 
var editor = $('.wysihtml5').each(function(i, elem) { 
    $(elem).wysihtml5({ 
     "font-styles": false, //Font styling, e.g. h1, h2, etc. Default true 
     "emphasis": true, //Italics, bold, etc. Default true 
     "lists": true, //(Un)ordered lists, e.g. Bullets, Numbers. Default true 
     "html": false, //Button which allows you to edit the generated HTML. Default false 
     "link": false, //Button to insert a link. Default true 
     "image": false, //Button to insert an image. Default true, 
     "color": false //Button to change color of font 
    }); 
}); 

function onChange() { alert("The content of the editor has changed"); }; 
editor.on("change", onChange); 

}

回答

1
$('.textarea').wysihtml5({ 
    events: { 
     change: function() { 
      var html = this.textarea.getValue(); 
      //ajax method 
     } 
    } 
} 
0

的好辦法趕上change事件:

editor.composer.element.addEventListener("keyup", myOnChange); 
editor.on("aftercommand:composer", myOnChange); 

但我不認爲你的每一個都很好,wysihtml5不能在多個可編輯部分上工作,在這裏你使用引導版,但我認爲它是一樣的。 這主要是爲什麼我寫jquery.scribe

所以,也許這樣?

$('.wysihtml5').each(function(i, elem) { 
    var editor = $(elem).wysihtml5({ 
     "font-styles": false, //Font styling, e.g. h1, h2, etc. Default true 
     "emphasis": true, //Italics, bold, etc. Default true 
     "lists": true, //(Un)ordered lists, e.g. Bullets, Numbers. Default true 
     "html": false, //Button which allows you to edit the generated HTML. Default false 
     "link": false, //Button to insert a link. Default true 
     "image": false, //Button to insert an image. Default true, 
     "color": false //Button to change color of font 
    }); 

    editor.composer.element.addEventListener("keyup", myOnChange); 
    editor.on("aftercommand:composer", myOnChange); 
}); 
0

我附加了聽衆這樣的:

$("#msgtextarea").data("wysihtml5").editor.on("change",function(){ 
    // Do your stuff 
}) 

希望它幫助。