【问题标题】:OnClick select all text in text_field railsOnClick 选择 text_field rails 中的所有文本
【发布时间】:2010-12-28 10:34:51
【问题描述】:

是否有一种 Rails 方法可以让 text_field 在单击时选择所有文本?如果不是,我怎样才能让 JavaScript 添加这种类型的功能?

<% text_field value => "highlight this", :after => "onClick="SelectAll('txtfld');"" %>  

【问题讨论】:

    标签: ruby-on-rails select onclick textfield


    【解决方案1】:

    我推荐阅读这篇文章:

    highlight text on click textbox

    这正是你想要的。

    <% form_for @foo do |f| %>
      <%= f.text_field :bar, :onclick => 'this.select();' %>
         or
      <%= text_field_tag :bar, nil, :onclick => 'this.select();' %>
    <% end %>
    

    【讨论】:

      【解决方案2】:

      如果你使用 jQuery,这非常简单方便。

      将此添加到您的 javascript 文件中:

      jQuery(document).ready(function($) {
        $(".select_all_text").focus(function() {
          this.select();
        });
      });
      

      之后,将类名select_all_text 添加到您希望具有该功能的字段中:

      <%= f.text_field :test, :class => "select_all_text" %>
      

      【讨论】:

      • 如果文本字段已经附加了 css 类样式,这会起作用吗?
      • 您可以附加多个 css 类。你只能有一个id。通过空格分隔多个类 class="class_one class_two"
      • 当然可以。例如。 &lt;%= f.text_field :test, :class =&gt; "class1 class2 select_all_text other_class" %&gt;
      猜你喜欢
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多