yii2中位于单独位置的单选按钮组 [英] Radio Button group in separate place in yii2

查看:71
本文介绍了yii2中位于单独位置的单选按钮组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想在单独的位置有两个单选按钮,我一直在尝试搜索解决方案,每个人都建议使用在我的情况下无法使用的单选列表。

So, I want to have two radio button in separate place, I have been trying to search for the solution and everyone suggests to use radiolist which is not possible in my case.

如果我这样说( work_part_time按钮):(下方)

If I put it like this (work_part_time button) : (below)

<div class="row">
    <div class="col-sm-2">
        <?= $form->field($model, 'work_part_time')->radio(['label' => 'yes', 'value' => 1])?>
    </div>-

    <div class="col-sm-3">
        <?= $form->field($model, 'hour_week')->textInput(['type' => 'number', 'placeholder' => 'Hour/Week'])->label(false)?>
    </div>

    <div class="col-sm-3">
        <?= $form->field($model, 'part_time_rate')->textInput(['type' => 'number', 'placeholder' => 'rate/hour(SGD)'])->label(false)?> 
    </div>
</div>

<div class="form-group">
    <?= $form->field($model, 'work_part_time')->radio( [0 => 'No'])->label('No')?>
</div>
<hr>
<div class="row">
    <div class="col-sm-2">
        <?= $form->field($model, 'work_part_time')->radio(['label' => 'yes', 'value' => 1])?>
    </div>-

    <div class="col-sm-3">
        <?= $form->field($model, 'hour_week')->textInput(['type' => 'number', 'placeholder' => 'Hour/Week'])->label(false)?>
    </div>

    <div class="col-sm-3">
        <?= $form->field($model, 'part_time_rate')->textInput(['type' => 'number', 'placeholder' => 'rate/hour(SGD)'])->label(false)?> 
    </div>
</div>

<div class="form-group">
    <?= $form->field($model, 'work_part_time')->radio( [0 => 'No'])->label('No')?>
</div>
<hr>

我只能获得0。

有人找到了解决方案吗?

Anyone has found the solution for this?

推荐答案

Yii会根据单选按钮将选中或未选中的值分配给单选按钮存储的属性的值,因此,如果该值为0,它将检查值为0的按钮。您的问题似乎是Yii自动生成的隐藏输入。正如其他人所建议的那样,如果您要在同一字段中使用多个单选按钮,则需要将其设置为 null

Yii will assign a checked or unchecked value to the radio button depending on the value of the stored attribute, so if the value is 0 it will check the button that has the value 0. Your problem seems to have been the hidden input that Yii automatically generates. As others have suggested, you need to set this to null if you want more than one radio button for the same field.

如果用户选中另一个按钮,则所有其他具有相同名称的单选按钮将变为未选中状态。属性名称由Yii创建按钮时自动生成。
为您的单选按钮尝试以下操作:

If the user checks another button, then all other radio buttons with the same name will become unchecked. The name of the attribute is generated automatically by Yii when it creates the button. Try these for your radio buttons:

<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 1', 'value' => 1, 'uncheck' => null]) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 2', 'value' => 0, 'uncheck' => null]) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 3', 'value' => 2, 'uncheck' => null]) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option4', 'value' => 3, 'uncheck' => null]) ?>

每个按钮需要一个不同的值,这是当记录已保存。

Each button needs a different value, and this is the value that will be stored in your field when the record is saved.

只能检查一个按钮,因此,如果您有多个具有相同值和相同名称的按钮,就像您在您的示例,则仅检查集合中的最后一个。我不知道如何解决这个问题。我建议您使用< formgroup> 将表单分成逻辑部分,每个部分都与work_part_time是否为yes有关。您似乎已经开始这样做!

There can only ever be one button checked, so if you have multiple buttons with the same value, and the same name, as you seem to have in your examples, then only the last one in the set will be checked. I don't know of a way round this. I suggest you use <formgroup> to split up your form into logical sections, each section relating to whether work_part_time is yes or no. You seem to have started doing this!

这篇关于yii2中位于单独位置的单选按钮组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆