OnClick 选择 text_field 轨道中的所有文本

发布于 2024-10-09 05:49:02 字数 199 浏览 0 评论 0原文

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

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

is there a rails way to enable a text_field to select all text upon clicking on it? If not how can I enable the JavaScript to add that type of functionality?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕头说它不想醒 2024-10-16 05:49:02

我建议阅读这篇文章:

在点击文本框中突出显示文本

这正是您想要的。

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

I recommend reading this article:

highlight text on click textbox

It is exactly what you want.

<% form_for @foo do |f| %>
  <%= f.text_field :bar, :onclick => 'this.select();' %>
     or
  <%= text_field_tag :bar, nil, :onclick => 'this.select();' %>
<% end %>
盗琴音 2024-10-16 05:49:02

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

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

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

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

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

If you are using jQuery, this is very easy and convenient.

Add this to your javascript file:

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

After that, add the class name select_all_text to the fields you want to have the functionality:

<%= f.text_field :test, :class => "select_all_text" %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文