开发者

ROR + Detect ClassName from text-field & Replace it by another one

开发者 https://www.devze.com 2023-01-28 15:39 出处:网络
In TextField of Ruby Code, I want to check whether the specified class is applied or not, on blur event. Please suggest some technique or way. Sample code is given below used by me.

In TextField of Ruby Code, I want to check whether the specified class is applied or not, on blur event. Please suggest some technique or way. Sample code is given below used by me.

Ruby Code for TextField::

    <%=f.text_field :Username, :size=>4, :tabindex=>2, :class=>class_name, 
:autocomplete => "off", :onblur=> $$$$$ %>

Suppose class_name : 'changeView' is applied on above textfield, then I have to detect this particular class & replace by another one like 'changeExtravi开发者_如何学JAVAew'. By using any of the javascript, we can replace that class name but suggest if you have any better option.

Thanks


Just a guessed answer...

So your class_name might be contain a value of changeView, and if in the case, you want to replace the changeView class name to changeExtraview, right?

If it is, then the following should be ok:

<%=f.text_field :Username, :size=>4, :tabindex=>2, :class=>class_name.gsub(/changeView/, "changeExtraview"), :autocomplete => "off", :onblur=> $$$$$ %>

========================

Another try:

<%=
  f.text_field :Username, :size=>4, :tabindex=>2, :class=>class_name, :autocomplete => "off",
    :onblur => "if($(this).hasClass('changeView')) {$(this).removeClass('changeView'); $(this).addClass('changeExtraview')}"
%>

========================

As @Faisal pointed out, using binding in JS side would be better. Although the answer was long time ago, I decided to update the answer again:

<%= f.text_field :Username, :size => 4, :tabindex => 2, :class => class_name, :autocomplete => "off" %>

And in any ways (I use content_for in this example), add the following js code:

<%= content_for :javascripts do %>
  <script type="text/javascript">
    jQuery(document).ready(function($) {
      $("<%= class_name %>").bind("blur", function() {
        var $this = $(this);
        if ($this.hasClass('changeView')) {
          $this.removeClass('changeView');
          $this.addClass('changeExtraView');
        }
      });
    });
  </script>
<% end %>

Note that the above code requires you have <%= yield :javascripts %> in your layout file,

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号