簡體   English   中英

如何觸發best_in_place事件

[英]How to trigger best_in_place events

我查看了整個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時,它不是。

我從這里獲得了一個很好的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函數,它基本上添加和刪除樣式的類。 使用上面的代碼,當我在best_in_place textarea中按best_in_place:update鍵時,我綁定到best_in_place:update函數的函數觸發,但沒有數據提交給服務器,正如我在控制台中看到的那樣。

TL; DR; 如何在按Enter鍵時觸發best_in_place提交/更新和停用,就像在可編輯區域外單擊時一樣?

$( '身體')點擊(); 也許? 沒有使用過這個框架,但是如果沒有提交按鈕並且輸入的重點丟失了提交表單,可能會有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM