javascript Yii2 - 如何在 activeform 中添加 onchange 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27120787/
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-10-28 06:58:26  来源:igfitidea点击:

Yii2 - How to add onchange event in activeform

javascriptyii2

提问by Kartz

This is my code :

这是我的代码:

<?= $form->field($model, 'int_roomCatId')
          ->dropDownList(ArrayHelper::map(TblCategory::find()->all(), 'int_category_id', 'str_category'), array('class' =>'form-control','prompt'=>'Select Room Category'))
          ->label('Room Category');  ?>

I want to add onchange = "getData()" event. where to add this?

我想添加 onchange = "getData()" 事件。在哪里添加这个?

回答by Ali MasudianPour

In your htmloptionsarray just do like below:

在您的htmloptions阵列中,只需执行以下操作:

dropDownList(ArrayHelper::map(TblCategory::find()->all(), 'int_category_id', 'str_category'), array('onchange'=>'getData()','class' =>'form-control','prompt'=>'Select Room Category'))

Each key and value in htmloptionsarray will be converted to html attributes, for example:

htmloptions数组中的每个key和value都会被转换成html属性,例如:

'key'=>'value'

Will be shown as :

将显示为:

<tag key="value" /> 

回答by user1780370

You can call using below:

您可以使用以下方式拨打电话:

<?= $form->field($model, 'product_name')->dropDownList(ArrayHelper::map(Products::find()->all(), 'id', 'name'), 
             ['prompt'=>'-Choose a Product-',
              'onchange'=>'
                $.get( "index.php?r=suborders/listprice&id="+$(this).val(), function( data ) {
                  $( "#suborders-product_price" ).val( data );
                });
            ']);
    ?>

Hope this help you.

希望这对你有帮助。

回答by Andrew Jenkins

Here's how I did it:

这是我如何做到的:

    echo $form->field($flightSearchForm, "lastDepartTimeChange", 
        ['options' => ['class' => 'col-xs-12', 
                       'onchange' => "changeHidden(\"departFlightTimeMin\")"]
         ])->widget(\yii\jui\SliderInput::classname());

As you can see, I just put it in the HTML options array which is the third input to the $form->field function. Hope this helps!

如您所见,我只是将它放在 HTML 选项数组中,这是 $form->field 函数的第三个输入。希望这可以帮助!