使用表单助手设置 tabindex 的自动方法 [英] Automatic method to set the tabindex using form helpers

查看:29
本文介绍了使用表单助手设置 tabindex 的自动方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 中使用表单助手时,是否有一种简单的方法可以让表单助手自动设置 tabindex 参数?

Is there an easy way to have the form helpers set the tabindex parameter automatically when using form helpers in Rails?

基本上,我不想在构建表单时手动设置每个表单元素的标签索引(我总是忘记在更改内容时更新它们).我编写的大多数表单基本上都是一个字段列表.标签索引应该按照它们定义的顺序排列.理想情况下,我会在 form_for 调用中设置初始索引,其他一切都会为我处理.

Basically, I don't want to have to manually set the tab index on every form element when building forms (I keep forgetting to update them when I change things). The majority of the forms I write are basically a list of fields. The tab index should be in the order they are defined. Ideally, I would set the initial index in the form_for call and everything else would be handled for me.

有人知道怎么做吗?

推荐答案

我通常会在 ApplicationHelper

def autotab
  @current_tab ||= 0
  @current_tab += 1
end

然后在我的视图中,我使用 :tabindex => 调用助手.autotab 像这样:

Then in my views I make calls to the helper with a :tabindex => autotab like so:

<%= text_field "post", "login",:tabindex => autotab, :value => @login %>

您还可以一次修改所有 text_fieldcheck_box 方法以自动添加 tabindex,方法是向您的应用程序助手添加如下内容:(未经测试但你明白了)

You can also modify all the text_field, check_box, methods one at a time to add the tabindex automatically, by adding something like this to your application helper: (untested but you get the point)

def text_field_with_tabindex(*args)
  options = args.last
  options[:tabindex] = autotab if options.is_a?(Hash) && options[:tabindex].nil?

  text_field_without_tabindex(*args)
end

def self.included(base)
  base.class_eval do
    alias_method_chain :text_field, :tabindex
  end
end

这可能比它的价值更麻烦

That might be more trouble than it's worth

这篇关于使用表单助手设置 tabindex 的自动方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆