在我的下拉列表中,当我选择百分比
时,“vendor 佣金”
(下一个字段)的标签应为“百分比”
。当我选择金额
时,标签应为“金额”
if($model->userType!="ADMIN" && $model->userType!="HOST"){
echo $form->field($model, 'fixedOrPercentagevendor')
->dropDownList(
[0=>'Amount',1=>'Percentage'],
[
'onchange'=>'if($(this).val() == 1){
$("#percentage").val("20");
}
else{
$("#percentage").val("1000");}'
]);
echo $form->field($model, 'vendorcommision')
->label('Vendor Fee')
->textInput(['type' => 'number','id' => 'percentage']);
}
最佳答案
这是我的做法:
- 关闭“vendor 佣金”标签 [
->label(false)
] - 使用跨度作为自定义标签,例如
<span class="custom-label"></span>
将事件监听器添加到下拉列表以更改标签。其内容如下:
var onChangeFunc = function(event) { if($(fixedOrPercentagevendorSelector).val() == 1) { $('.custom-label').html('Percentage'); } else { $('.custom-label').html('Amount'); } }
注意:如果您的项目包含 I18N 支持,您可能必须将标签保存在某个变量中并使用它们。
关于javascript - Yii2 activeform onchange 标签名称相同的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48813741/