如何触发best_in_place事件

时间:2014-03-11 01:52:18

标签: javascript jquery ruby-on-rails ruby-on-rails-4 best-in-place

我查看了Google和StackOverflow,并查看了best_in_place javascript代码无效。

我使用best_in_place通过使用textarea来编辑Post模型的属性,如下所示:

<%= best_in_place @post, :summary, 
                 { :type => :textarea, :display_with => 'simple_format'}  %>

我希望能够在按下Enter / Return时触发best_in_place的提交功能,除非用shift键(允许回车)按下它,就像在可编辑区域外点击一样。使用常规文本/输入,这是默认功能(减去回车部分)。但是,当使用textareas时,它不是。

我从here获取了一个很好的jQuery,它可以处理按下Enter时有/没有Shift的情况,但我无法弄清楚如何触发best_in_place来提交数据。

//Have to bind it to the activate function, or else 
//the element won't be in the DOM when it tries to identify it
$('.best_in_place').bind('best_in_place:activate', function(){          
   $(this).find('textarea').keyup(function(e) {
      var $textarea = $(this);
      if(e.keyCode == 13 && !e.shiftKey) {
        e.preventDefault(); // Don't make a new line         
        // This line does weird things
       $(this).parents('.best_in_place').trigger('best_in_place:update').trigger('best_in_place:deactivate');
      }
    });
});

我还有一些绑定到best_in_place:update功能的功能,基本上添加&amp;删除样式的类。使用上面的代码,当我在best_in_place textarea中输入时,我绑定到best_in_place:update函数的函数会触发,但没有数据提交给服务器,正如我在控制台中看到的那样。

TL; DR; 如何在点击Enter键时触发best_in_place提交/更新和停用,就像在可编辑区域外点击一样?

1 个答案:

答案 0 :(得分:0)

$( '主体')点击();也许?没有使用过这个框架,但是如果没有提交按钮并且输入的重点丢失了提交表单,可能会有效。