Yii2:如何在checkboxList()中为每个复选框分配一个id [已关闭]

gijlo24d  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(158)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答案。

这个问题是由一个打字错误或一个无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
两年前就关门了。
Improve this question
我用Yii2创建了一个checkboxList,我需要给每个checkbox分配一个id(这样我就可以在jQuery中使用它了),但是我不知道怎么做。

<?
$cities ArrayHelper::map(Cities::find()->all(), 'city_id', 'city_name');
echo Html::checkboxList(
    'cities'
    null,
    $cities,
    [
        'itemOptions' => [
            'id' => $cities->city_id // MY PROBLEM IS IN THIS LINE,
        ],
    ]
) ?>

这是错误:

1rhkuytd

1rhkuytd1#

如果你在这里检查文档,你可以给$options的(yii\helpers\Html::checkboxList的第四个参数)item键传递一个匿名函数,在这里你可以显式地指定HTML呈现结构到你的复选框列表,如下所示:

<?= Html::checkboxList('cities', null, $cities, [
        'item' => function($index, $label, $name, $checked, $value) use ($cities) {
            return "<label class='checkbox col-md-4'>
                        <input type='checkbox' 
                               {$checked} 
                               name='{$name}'
                               value='{$value}' 
                               id='" . $cities[$index]->city_id . "'>
                        {$label}
                   </label>";
        }
    ]);
?>
bfnvny8b

bfnvny8b2#

<?
$cities ArrayHelper::map(Cities::find()->all(), 'city_id', 'city_name');
echo Html::checkboxList(
    'cities'
    null,
    $cities,
    [
        'itemOptions' => [
            'id' => $cities->city_id // MY PROBLEM IS IN THIS LINE,
        ],
    ]
) ?>

应该是

<?= Html::checkboxList('cities', null, ArrayHelper::map(Cities::find()->all(), 'city_id', 'city_name')) ?>

相关问题