Yii2 - 通过SwitchInput小部件更改DataPicker小部件选项

时间:2015-09-11 12:37:36

标签: php jquery yii2

DataPicker小部件:

<?= $form->field($model, 'dateSale')->textInput()->widget(DatePicker::classname(), [
        'name' => 'dp_4',
        'type' => DatePicker::TYPE_COMPONENT_PREPEND,
        'disabled' => true
        ]) ?>

SwitchInput小部件:

<?= $form->field($model, 'status')->textInput()->widget(SwitchInput::classname(), [
    'name' => 'status_11',
    'pluginEvents' => [
        'switchChange.bootstrapSwitch' => 'function(event, state) {
            if (state == 0){
                $("#vehicle-datesale").dp4("disabled", "false");
            }
            else {
                $("#vehicle-datesale").dp4("disabled", "true");
            }   
        }',
    ]
]) ?>

为什么不进行上述工作,我该怎么做?

2 个答案:

答案 0 :(得分:1)

您需要更改SwitchInput小部件中的代码。

use yii\helpers\Html;

<?= $form->field($model, 'status')->textInput()->widget(SwitchInput::classname(), [
        'name' => 'status_11',
        'pluginEvents' => [
            'switchChange.bootstrapSwitch' => 'function(event, state) {
                if (state == 0 || state == false){
                    $("#'.Html::getInputId($model, 'dateSale').'").attr("disabled", "false");
                }
                else {
                    $("#'.Html::getInputId($model, 'dateSale').'").attr("disabled", "true");
                } 

            }',
        ]
    ]) ?>

答案 1 :(得分:0)

你的代码正在运作;只有改变:

 if (state == 0 || state == false){
                $("#'.Html::getInputId($model, 'dateSale').'").attr("disabled", "false");
            }
            else {
                $("#'.Html::getInputId($model, 'dateSale').'").attr("disabled", "true");
            } 

通过

 if (state == 0 || state == false){
                $("#'.Html::getInputId($model, 'dateSale').'").attr("disabled", false);
            }
            else {
                $("#'.Html::getInputId($model, 'dateSale').'").attr("disabled", true);
            }