[英]yii2 kartik switch input type radio button get value in javascript
我将yii2 kartik开关输入与radio类型一起使用,我想在javascript中(特别是在JsExpression中)获取选定选项的值,这是我的代码:
$model->orientation = 'Portrait';
echo $form->field($model, 'orientation')->widget(SwitchInput::classname(), [
'name' => 'information_orientation',
'type' => SwitchInput::RADIO,
'value' => 'Portrait',
'items' => [
['label' => 'Portrait   ', 'value' => 'Portrait'],
['label' => 'Paysage   ', 'value' => 'Paysage'],
],
'pluginOptions' => [
'onText' => 'Oui',
'offText' => 'Non',
'onColor' => 'success',
'offColor' => 'danger',
'size' => 'mini'
],
'labelOptions' => ['style' => 'font-size: 13px'],
]);
我努力了 :
$([name='information-orientation']).val()
但是它返回了一个未定义的值
SwitchInput
类不关心您为其提供的name属性
根据他们的文档,您需要将name
包装在options
数组中
echo $form->field($model, 'orientation')
->widget(SwitchInput::classname(), [
'options' => ['name' => 'information_orientation'],
'type' => SwitchInput::RADIO,
....
来自docs :
options
: 排列小部件输入标记的HTML属性。
编辑:
然后您可以使用以下内容
// to get value:
$("[name='information_orientation']").val()
// to check if the switch is or on off
$("[name='information_orientation']").prop('checked')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.