当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Jquery to disable previous radio buttons in Yii2

问题描述:

<?= $form->field($model, 'status')->radioList(array('1'=>'Approved','2'=>'Digital','3'=>'CDP','4'=>'Print','5'=>'Other Process','6'=>'Packing','7'=>'Dispatch',)); ?>

I'm trying to implement status update form. I want to know how can I disable previous radio buttons.

e.g-

If current status is CDP then Status "Approved" and "Digital" should be disable.

how to write java script for this, im implementing in Yii2 Framework.

网友答案:

Try to listen if someone choose a radio button. Than '.disable' on every button with '.each' until you reached the key.

edit: i hate to write it blind but try this:

$('#radioButtons').on('change', function(){
  var val = this.value;
  $.each(arrayname, function( index, value ){
     if(index < val){
        value.disable();
     }else{
        value.enable();
     }
  });
});

if the selected value is smaller, it will be disable, otherwise it will be anabled. but if you do it this way you can´t change your choice to a button above ? is this really what you want ?

网友答案:

Since your option will already be set you can use Yii2 to do this. You will need to manually set the input property "item" to do this.

<?=$form->field($model, 'status')->radioList(['1' => 'Approved', '2' => 'Digital', '3' => 'CDP', '4' => 'Print', '5' => 'Other Process', '6' => 'Packing', '7' => 'Dispatch'], ['item' => function($index, $label, $name, $checked, $value) {$checked = $checked == 1 ? 'checked=""' : 'disabled=""';echo "<label><input tabindex='{$index}' type='radio' {$checked}'name='{$name}'value='{$value}'> {$label}</label>";}]);?>

The above will leave the selected one checked and disable the others using the HTML options "checked" and "disabled" respectively.

分享给朋友:
您可能感兴趣的文章:
随机阅读: