在 simple_form 中禁用 copy/past 功能

Disable copy/past functionality in simple_form

我正在尝试禁用 simple_form 输入字段中的 copy/paste 功能。我知道有人问 before outside of a simple_form context. I added a class to the form itself following the ideas from this post,并在 application.js 的函数中使用了 class。这目前没有产生所需的功能——在 simple_form 中是否有一种特殊的方法可以做到这一点?

(我知道不建议篡改默认浏览器功能,但复制 'by hand' 的操作与我试图通过我的应用实现的目标一致)。

在视图中:

<%= simple_form_for @word, url: lesson_word_exposition_path(current_lesson, @word.word), html: { class: "disablecopypaste"}, method: :patch do |f| %>
  <%= f.input :term_given_by_student, label: "Enter the term exactly as above:" %><br>
  <%= f.button :submit, class: 'btn btn-primary' %>
<% end %>

来自application.js:

$(document).ready(function () {
    $('input.disablecopypaste').bind('copy paste', function (e) {
       e.preventDefault();
    });
  });

您可以尝试使用用户-select css 属性.

.unselectable : {
  -webkit-user-select: none;  
  -moz-user-select: none;    
  -ms-user-select: none;      
  user-select: none;          
}


$(document).ready(function () {
    $('input.disablecopypaste').toggleClass('unselectable')
  });