如何使复选框默认为“已选中"?在 Rails 1.2.3 中? [英] How can I make a check box default to being "checked" in Rails 1.2.3?

查看:44
本文介绍了如何使复选框默认为“已选中"?在 Rails 1.2.3 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使复选框在最初显示时默认为已选中"?

How can I make a check box default to being "checked" when it is initially displayed?

我还没有找到一种Rails"方式来做到这一点(有效),所以我用 JavaScript 做到了.在 Rails 中有正确的方法来做到这一点吗?我使用的是 Rails 1.2.3.

I've not found a "Rails" way to do this (that works) so I did it with JavaScript. Is there a proper way to do this in Rails? I'm using Rails 1.2.3.

推荐答案

如果您在表单上下文中使用 check_box,那么复选框将显示该字段具有的任何值.

If you're using check_box in the context of a form, then the check box will show whatever value that field has.

@user = Person.find(:first)
@user.active = true
check_box 'user', 'active'  #=> will be checked by default

如果您使用 check_box_tag,第三个参数是初始状态 (check_box_tag doc):

If you're using check_box_tag, the third parameter is the initial state (check_box_tag doc):

check_box_tag "active", 1, true

这篇关于如何使复选框默认为“已选中"?在 Rails 1.2.3 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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