2012-12-09 55 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 
}) 

希望它帮助。