/** * 单选输入框 * * @param $attrs array 控件参数 * @return string 控件代码 */ public static function radio($attrs) { $options = arr::take('options', $attrs); $options = field::_option($options); $value = arr::take('value', $attrs); $attrs['class'] = isset($attrs['class']) ? 'radio ' . $attrs['class'] : 'radio'; //默认样式inline,允许传入block使得checkbox每个元素显示一行 $valid = arr::take('valid', $attrs); $html[] = '<ul' . html::attributes($attrs) . '>'; if (is_array($options)) { $i = 1; foreach ($options as $val => $text) { $checked = $val == $value ? ' checked="checked"' : ''; //这儿代码可能有问题,请检查 $html[] = ' <li>'; $html[] = ' <input type="radio" name="' . $attrs['name'] . '" id="' . $attrs['name'] . '-item' . $i . '" value="' . $val . '"' . $checked . '' . (isset($valid) && $i == 1 ? ' valid = "' . $valid . '"' : '') . '/>'; $html[] = ' <label for="' . $attrs['name'] . '-item' . $i . '">' . html::encode($text) . '</label>'; $html[] = ' </li>'; //这儿代码不完美 $i++; } } $html[] = '</ul>'; if (isset($valid)) { $html[] = '<label for="' . $attrs['name'] . '" class="error">' . $attrs['title'] . '</label>'; } return implode("\n", $html); }