checkBox
用途
创建一个 checkbox 在表单域form field.所有常见的HTML元素适用.
示例
<g:checkBox name="myCheckbox" value="${true}" />
描述
属性
name
- checkbox的名称
value
(可选) - checkbox的值
checked
(可选) - 表达式为true , checkbox 被选中
checkBox标记通常只需要name value Value属性,并推断的复选框是否被选中取值. 然而, 如果覆盖这个行为, checked
属性能被很好的控制.
来源
Show Source
def checkBox = {attrs ->
attrs.id = attrs.id ? attrs.id : attrs.name
def value = attrs.remove('value')
def name = attrs.remove('name')
def disabled = attrs.remove('disabled')
if (disabled && Boolean.valueOf(disabled)) {
attrs.disabled = 'disabled'
} // Deal with the "checked" attribute. If it doesn't exist, we
// default to a value of "true", otherwise we use Groovy Truth
// to determine whether the HTML attribute should be displayed
// or not.
def checked = true
if (attrs.containsKey('checked')) {
checked = attrs.remove('checked')
} if (checked instanceof String) checked = Boolean.valueOf(checked) if (value == null) value = false // the hidden field name should begin with an underscore unless it is
// a dotted name, then the underscore should be inserted after the last
// dot
def lastDotInName = name.lastIndexOf('.')
def hiddenFieldName = lastDotInName == -1 ? '_' + name : name[0..lastDotInName] + '_' + name[(lastDotInName+1)..-1] out << "<input type=\"hidden\" name=\"${hiddenFieldName}\" /><input type=\"checkbox\" name=\"${name}\" "
if (value && checked) {
out << 'checked="checked" '
}
def outputValue = !(value instanceof Boolean || value?.class == boolean.class)
if (outputValue)
out << "value=\"${value}\" "
// process remaining attributes
outputAttributes(attrs) // close the tag, with no body
out << ' />' }