checkboxList使用arrayHalper并获取值yii2

时间:2015-11-06 17:20:05

标签: html checkbox yii2

这是我的表单checkboxList with arrayhalper:

<div class="form-group">
    <?= $form->field($model, 'reg_id')->checkboxList(
        arrayHelper::Map(Riders::find()->where(['user_id' => Yii::$app->user->identity->id])->all(),'rider_id',
            function($model) { return **$model->rider_firstname . " (" . $model->cagoriesCategory->category_name . ")"**; }),
        ['class' => 'checkbox-inline', 'id' => 'person' ]), ?>
</div>

标签正常工作,例如:Jilly (MasterA)。但我希望通过标签获得价值差异,例如Jilly:MasterA。如何给出这样的价值?

2 个答案:

答案 0 :(得分:1)

您需要更改数组值语法。像是,

<div class="form-group">
    <?= $form->field($model, 'reg_id')->checkboxList(
        arrayHelper::Map(Riders::find()->where(['user_id' => Yii::$app->user->identity->id])->all(), function($model) { return $model->rider_firstname . " : " . $model->cagoriesCategory->category_name; },
            function($model) { return $model->rider_firstname . " (" . $model->cagoriesCategory->category_name . ")"; }),
        ['class' => 'checkbox-inline', 'id' => 'checkbox' ]); ?>
</div>

答案 1 :(得分:1)

它的工作我想要的,感谢@GAMITG

<div class="form-group">
    <?= $form->field($model, 'reg_id')->checkboxList( 
    arrayHelper::Map(Riders::find()->where(['user_id' => Yii::$app->user->identity->id])->all(), function($model) { return $model->rider_firstname . ":" . $model->cagoriesCategory->category_name; }, 
        function($model) { return $model->rider_firstname . " (" . $model->cagoriesCategory->category_name . ")"; }), 
    ['item' => function($index, $label, $name, $checked, $value) { 
        $return = '<label>'; 
        $return .= '<input type="checkbox" name="' . $name . '" value="' . $value . '" class="checkbox-inline" id="person1">';
        $return .= '<span>' . ucwords($label) . '</span>'; 
        $return .= '</label>'; 
        return $return; 
    } ]); ?>
</div>

然后javascript代码cek it:

$('[id="person1"]:checked').each(function() {
    var arr = $(this).val();
    alert(arr);
});