Yii:单选按钮给出0值

时间:2014-12-01 07:36:40

标签: php yii radio-button radiobuttonlist

我正在使用单选按钮从可能的选项数量中选择正确的选项。目前我对问题陈述有四种选择,但可能的答案选项可能会增加。我面临的问题是以下代码仅适用于最后一个选项,否则它在表单提交时给出0值。另外请建议我如何使用radioButtonList和以下代码,因为我想在不同的DIV中显示单选按钮。请检查以下代码和建议。在此先感谢

<div class="form-group" id="statement_div">
    <?php echo $form->labelEx($model,'statement', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'statement',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control')); ?>
        <?php echo $form->error($model,'statement'); ?>
    </div>

    <div class="clearfix"></div>
</div>


<div class="form-group" id="option1_div">
    <?php echo $form->labelEx($model,'option_1', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_1',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control', 'readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_1'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'1')) . ''; ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option2_div">
    <?php echo $form->labelEx($model,'option_2', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_2',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control','readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_2'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'2')) . ''; ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option3_div">
    <?php echo $form->labelEx($model,'option_3', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_3',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control', 'readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_3'); ?>
    </div>
     <?php echo $form->radioButton($model,'correct_option',array('value'=>'3')) . ''; ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option4_div">
    <?php echo $form->labelEx($model,'option_4', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_4',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control','readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_4'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'4')) . ''; ?>
    <div class="clearfix"></div>
</div>

2 个答案:

答案 0 :(得分:1)

如果您没有使用radioButtonList,则必须将每个radioButton的uncheckValue设置为null

http://www.yiiframework.com/wiki/315/using-multiple-radio-button/

<div class="form-group" id="statement_div">
    <?php echo $form->labelEx($model,'statement', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'statement',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control')); ?>
        <?php echo $form->error($model,'statement'); ?>
    </div>

    <div class="clearfix"></div>
</div>


<div class="form-group" id="option1_div">
    <?php echo $form->labelEx($model,'option_1', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_1',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control', 'readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_1'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'1','uncheckValue' => null)); ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option2_div">
    <?php echo $form->labelEx($model,'option_2', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_2',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control','readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_2'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'2','uncheckValue' => null))) . ''; ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option3_div">
    <?php echo $form->labelEx($model,'option_3', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_3',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control', 'readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_3'); ?>
    </div>
     <?php echo $form->radioButton($model,'correct_option',array('value'=>'3','uncheckValue' => null))) . ''; ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option4_div">
    <?php echo $form->labelEx($model,'option_4', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_4',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control','readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_4'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'4','uncheckValue' => null))) . ''; ?>
    <div class="clearfix"></div>
</div>

答案 1 :(得分:0)

因为activeRadioButton正在为每个复选框/无线电生成具有相同名称的无敌输入字段(因此以前的值会被表单数据中的此字段覆盖)。应用uncheckValue => null删除此字段。 请记住,如果不使用此字段,请不要选择任何收音机/复选框而不是表单数据,否则将无键correct_option

<?php
echo $form->radioButton(
    $model,
    'correct_option',
    array(
        'value' => '4',
        'uncheckValue' => null,
    )
);
?>