flsouto/htradios

该软件包最新版本(1.0.0)没有提供许可证信息。

生成单选按钮以选择单个选项。

1.0.0 2017-03-11 18:50 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:43:48 UTC


README

此库可以轻松生成单选按钮。建议您查看其父类的文档,以了解所有继承的功能

安装

通过composer

composer require flsouto/htradios

用法

在以下示例中,我们生成一个包含三个选项的选择列表。

<?php
require_once('vendor/autoload.php');
use FlSouto\HtRadios;

$select = new HtRadios("id_category");
$select->options([1=>'Category1',2=>'Category2',3=>'Category3']);

echo $select;

输出

<div class="widget 58c445e037fa2" style="display:block">
 <label>
 <input type="radio" name="id_category" value="1" />Category1</label>
 <br />
 <label>
 <input type="radio" name="id_category" value="2" />Category2</label>
 <br />
 <label>
 <input type="radio" name="id_category" value="3" />Category3</label>
 <br />
 <div style="color:yellow;background:red" class="error">
 </div>
</div>

注意:除了关联数组之外,options方法还接受其他格式。请查看HtChoice类的文档以了解更多信息。

更改分隔符

默认情况下,用于分隔选项的分隔符是一个<br/>元素,即换行符。但您可以使用separator方法更改它。在下面的示例中,我们将分隔符更改为两个空格,以便选项水平显示

<?php
require_once('vendor/autoload.php');
use FlSouto\HtRadios;

$select = new HtRadios("id_category");
$select->options([1=>'Category1',2=>'Category2',3=>'Category3'])
	->separator("&nbsp;&nbsp;");

echo $select;

输出

<div class="widget 58c445e039f00" style="display:block">
 <label>
 <input type="radio" name="id_category" value="1" />Category1</label>
 &nbsp;&nbsp;
 <label>
 <input type="radio" name="id_category" value="2" />Category2</label>
 &nbsp;&nbsp;
 <label>
 <input type="radio" name="id_category" value="3" />Category3</label>
 &nbsp;&nbsp;
 <div style="color:yellow;background:red" class="error">
 </div>
</div>

选择选项

如果您已阅读HtFieldHtWidget父类的文档,您已经知道应该使用context方法来设置字段/小部件的值

<?php
require_once('vendor/autoload.php');
use FlSouto\HtRadios;

$select = new HtRadios('id_category');
$select->options([1=>'Category1',2=>'Category2',3=>'Category3']);
$select->context(['id_category'=>2]);

echo $select;

输出

<div class="widget 58c445e03a67b" style="display:block">
 <label>
 <input type="radio" name="id_category" value="1" />Category1</label>
 <br />
 <label>
 <input type="radio" name="id_category" value="2" checked="checked" />Category2</label>
 <br />
 <label>
 <input type="radio" name="id_category" value="3" />Category3</label>
 <br />
 <div style="color:yellow;background:red" class="error">
 </div>
</div>