简体   繁体   中英

yii2 dynamic-form wbraganca call javascript function

I have read some articles about yii2 dynamic-form and javascript function. The solution given by InsaneSkull is perfect. But i have one question. example : i'm using dynamic-form from wbraganca and trying to call function onchange event (javascript). My code like this

    <?= $form->field($detail, "[{$i}]qty")->widget(\yii\widgets\MaskedInput::className(),
[
        'clientOptions' => [
                'alias' => 'numeric',
                'groupSeparator' => ',',
                'digits' => 0,
                'autoGroup' => true,
                'removeMaskOnSubmit' => true,
                'rightAlign' => false,                                                                  
        ],
        'options' => [
                'class' => 'form-control',
                'onchange' => 'Info($(this))',                                                                  
        ]                                                               
]) ?>

First, i try to register Info function like below

<?php
$script = <<< JS

function Info(item){
   var index  = item.attr("id").replace(/[^0-9.]/g, "");        
   alert(index);        
};

JS;
$this->registerJs($script);
?>

It gave error because Info function not defined yet.

Second, I registered in AppAsset and it worked.

My question : what is the differences? *(I think it was the scope). How to define the function beside register in AppAsset ?

Where dou you register above script? If in view after use widget, default position at which JS is register is POS_READY public void registerJs ( $js, $position = self::POS_READY, $key = null ) Try to use position POS_BEGIN or POS_HEAD if you want to put the script after use widget. Otherwise You can override widget class and put this script into init method of the widget which is execute before run method rendered widget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM