问题描述
所以,我想在不同的地方有两个单选按钮,我一直在尝试寻找解决方案,每个人都建议使用 radiolist,这在我的情况下是不可能的.
如果我这样说(work_part_time 按钮):(下)
<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' => '小时/周'])->标签(假)?><div class="col-sm-3"><?= $form->field($model, 'part_time_rate')->textInput(['type' => 'number', 'placeholder' => 'rate/hour(SGD)'])->标签(假)?>
<div class="form-group"><?= $form->field($model, 'work_part_time')->radio( [0 => 'No'])->label('No')?>
<小时><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' => '小时/周'])->标签(假)?>
<div class="col-sm-3"><?= $form->field($model, 'part_time_rate')->textInput(['type' => 'number', 'placeholder' => 'rate/hour(SGD)'])->标签(假)?>
<div class="form-group"><?= $form->field($model, 'work_part_time')->radio( [0 => 'No'])->label('No')?>
<小时>
我只能得到 0 的值.
有人找到了解决方案吗?
Yii 将根据存储属性的值为单选按钮分配选中或未选中的值,因此如果值为 0 它将检查按钮值为 0.您的问题似乎是 Yii 自动生成的隐藏输入.正如其他人所建议的,如果您希望同一字段有多个单选按钮,则需要将其设置为 null
.
如果用户选中另一个按钮,则所有其他同名的单选按钮将变为未选中状态.属性名称由 Yii 在创建按钮时自动生成.为您的单选按钮试试这些:
= $form->field($model, 'work_part_time')->radio(['label' => 'Option 1', 'value' => 1, '取消选中' => 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]) ?>
每个按钮需要不同的值,这是保存记录时将存储在您的字段中的值.
只能检查一个按钮,因此如果您有多个具有相同值和相同名称的按钮,就像您在示例中所看到的那样,那么只会检查集合中的最后一个.我不知道有什么办法解决这个问题.我建议您使用
将您的表单拆分为逻辑部分,每个部分都与 work_part_time 是 yes 或 no 相关.你似乎已经开始这样做了!
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.
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>
I only can get 0 for the value.
Anyone has found the solution for this?
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.
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.
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 中单独位置的单选按钮组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!