kartik-v / yii2-widget-switchinput

A Yii2 wrapper widget for the Bootstrap Switch plugin to use checkboxes & radios as toggle switches (sub repo split from yii2-widgets)
Other
38 stars 11 forks source link

Setting checked value on javascript function called #33

Open danisetg opened 5 years ago

danisetg commented 5 years ago

Hi, I am trying to use this widget and it is really useful, I really like it, but I'm facing the following problem: I have an activeForm in wich under certain conditions I call a "populate()" javascript function for filling the form fields, but I can't manage to change the switch to its 'on' property. I am using it as a checkbox in the following way:

$form->field($model, "es_usuario")->widget(SwitchInput::classname(), [
                                    'pluginOptions' => [
                                        'onText' => 'Sí',
                                        'offText' => 'No',
                                        'onColor' => 'success'
                                    ],
                                ])->label("Agregar como usuario del sistema?");

I have tried with:
$('#contactos-es_usuario').val(true); $('#contactos-es_usuario').prop('checked', true); $('#contactos-es_usuario').attr('checked', true); $('#contactos-es_usuario')[0].checked = true; With no results in any of these. When I check the element in the development tools, the checked property does change to true, but the switch remains the same.

I'd really appreciate give me any help with this. Thanks in advance

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/72060910-setting-checked-value-on-javascript-function-called?utm_campaign=plugin&utm_content=tracker%2F8186004&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F8186004&utm_medium=issues&utm_source=github).
zhangyc310 commented 5 years ago

method 1:

<?=$form->field($model, '论文类型')->widget(SwitchInput::classname(), [
    'name'               => 'lwlx',
    'type'               => SwitchInput::RADIO,
    'value'              => -1,
    'tristate'           => true,
    'items'              => [
        ['label' => '期刊论文', 'value' => '期刊论文', 'options' => ['checked' => true]],
        ['label' => '论文集论文', 'value' => '论文集论文'],
        ['label' => '报纸论文', 'value' => '报纸论文'],
        ['label' => '集刊论文', 'value' => '集刊论文'],
        ['label' => '其他文章', 'value' => '其他文章'],
    ],
    'pluginOptions'      => ['size' => 'mini'],
    'labelOptions'       => ['style' => 'font-size: 12px'],
]);

method2:

<?php
$js = <<<JS
jQuery(function($) {
$("input[value='期刊论文']").prop('checked', true);
});
JS;
$this->registerJs($js, \yii\web\View::POS_END);
?>