php yii2 中单独位置的单选按钮组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34216240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:51:09  来源:igfitidea点击:

Radio Button group in separate place in yii2

phpyii2active-form

提问by Robert Limanto

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.

所以,我想在不同的地方有两个单选按钮,我一直在尝试寻找解决方案,每个人都建议使用 radiolist,这在我的情况下是不可能的。

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

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

<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.

我只能得到 0 的值。

Anyone has found the solution for this?

任何人都找到了解决方案?

回答by Joe Miller

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 nullif you want more than one radio button for the same field.

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

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:

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

<?= $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!

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

回答by Nana Partykar

I've personally checked your code in my system. This code returning 0 or 1 (in respective of selection of radio button.) It's working Fine. You can use my code.

我已经在我的系统中亲自检查过你的代码。此代码返回 0 或 1(分别对应于单选按钮的选择)。它工作正常。你可以使用我的代码。

In below code, if you want to give labelas Work part timeor some other, put in ->label('Work Part Time');

在下面的代码中,如果您想将标签指定兼职工作或其他,请输入->label('Work Part Time');

.
. // Your code
<?= $form->field($model, 'work_part_time')->radioList([1 => 'yes', 0 => 'No'])->label('Work Part Time'); ?>  
.
. // Your code

AND

1)If you want to check 'Yes' as default checkedradio button, then you have to assign like this <?php $model->status_id = 1?>

1)如果你想勾选“”作为默认勾选的单选按钮,那么你必须像这样分配<?php $model->status_id = 1?>

.
. // Your code
<?php $model->status_id = 1?>
<?= $form->field($model, 'work_part_time')->radioList([1 => 'yes', 0 => 'No'])->label('Work Part Time'); ?> 
.
. // Your code

2)If you want to check 'No' as default checkedradio button, then you have to assign like this <?php $model->status_id = 0?>

2)如果你想勾选''作为默认选中的单选按钮,那么你必须像这样分配<?php $model->status_id = 0?>

.
. // Your code
<?php $model->status_id = 0?>
<?= $form->field($model, 'work_part_time')->radioList([1 => 'yes', 0 => 'No'])->label('Work Part Time'); ?>  
.
.//Your code

回答by Amitesh Kumar

try this:

尝试这个:

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

add 'uncheckValue'=>null in htmlOption array it will work.

在 htmlOption 数组中添加 'uncheckValue'=>null 它将起作用。

回答by Bumbuli

If you substitute "uncheck" with "uncheckValue", the voted answer will work, except it will only post the value of the last radio on the list if selected and 0 for the rest. To make it work and post selected values as intended, I added some JS code to it. Hope it will help someone down the road.

如果您用“uncheckValue”替换“uncheck”,则投票的答案将起作用,除非它仅在选择时发布列表中最后一个收音机的值,其余为0。为了使其工作并按预期发布选定的值,我向其中添加了一些 JS 代码。希望它会帮助有人走上这条路。

<!-- Complete solution for Radio buttons on separate places Yii2 together with its JS function-->
<php use  yii\web\View; ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 1', 'value' => 1, 'uncheckValue' => null,'onChange'=>' if($(this).prop("checked")){ var radioValue = 1;$(this).val(radioValue); var radioName = "Model[work_part_time]"; RadioSelected(radioName,radioValue);}else{$(this).val("")};']) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 2', 'value' => 0, 'uncheckValue' => null,'onChange'=>' if($(this).prop("checked")){ var radioValue = 0; $(this).val(radioValue); var radioName = "Model[work_part_time]"; RadioSelected(radioName,radioValue);}else{$(this).val("")};']) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 3', 'value' => 2, 'uncheckValue' => null,'onChange'=>' if($(this).prop("checked")){ var radioValue = 2; $(this).val(radioValue); var radioName = "Model[work_part_time]"; RadioSelected(radioName,radioValue);}else{$(this).val("")};']) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option4', 'value' => 3, 'uncheckValue' => null,'onChange'=>' if($(this).prop("checked")){var radioValue = 3; $(this).val(radioValue);  var radioName = "Model[work_part_time]"; RadioSelected(radioName,radioValue);}else{$(this).val("")};']) ?>
<?php
//JS Function
$RadioONSeparatePlaceJS = <<<JS
function RadioSelected(radioName,radioValue)
{
   $('input[name="' + radioName + '"]').val(radioValue);
}

JS;
$this->registerJs($RadioONSeparatePlaceJS,View::POS_HEAD);
?>

Thank you for the great answer Joe Miller.

感谢乔·米勒的精彩回答。