【发布时间】:2017-02-09 12:49:46
【问题描述】:
我正在尝试根据当前选中的单选按钮使用数据绑定来应用 CSS 样式。
以下是我尝试应用但不起作用的代码..
<input type="radio" value="mtn" data-bind="checked: setRadioVal, css: {lblstylebold: checkRadioEnabled(this)}" id="mtnradio">
<input type="radio" value="atn" data-bind="checked: setRadioVal, css: {lblstylebold: checkRadioEnabled(this)}" id="atnradio">
var ViewModel = {
setRadioVal: ko.observable(null),
checkRadioEnabled: function(value) {
if (value == ViewModel.setRadioVal())
return true;
else
return false;
}
}
ViewModel.setRadioVal("mtn") // This gets sets initially with either mtn or atn based on ajax response which I have not posted here. Just for understanding I have set as mtn.
因此,一旦用户选择了其中一个单选按钮,setRadioVal 就会使用 mtn 或 atn 进行更新。我正在尝试调用函数checkRadioEnabled 并在当前选择的单选按钮值等于启用值时返回true。但是css类没有得到应用。
当我调试时,我看到它在单击单选按钮时进入函数checkRadioEnabled,但值参数作为窗口对象出现。如何传递单选按钮值并在函数checkRadioEnabled 中访问它?
我在这里做错了吗?
【问题讨论】: