剔除检查 - 绑定到字符串文字
我有一个数据存储在具有关联数据类型(文本、复选框、密码等)的名称/值对中,
我尝试使用淘汰赛来绑定选中的属性,如下所示
<input type="checkbox" data-bind="checked: $root.Value">
我的问题是值作为字符串文字输入,所以我不是获取 (true) 的值,而是像这样 ("true") 得到它。无论 "true"/"false" 它会自动将其标记为 true。
我希望能够运行一个简单的函数来测试它是否为“true”/“false”,如果是,则返回布尔值,这样淘汰赛将正确绑定。
有什么建议吗?
更新:数据基本上采用以下格式
{
"ID": 276,
"Name": "DefaultIsCallToOrder",
"Value": "false",
"Sequence": 7,
"DataType": "checkbox",
},
{
"ID": 277,
"Name": "DefaultIsFeatured",
"Value": "false",
"Sequence": 8,
"DataType": "checkbox",
},
I have a data stored in Name/Value pairs with an associated data type (text, checkbox, password, etc.,)
I am trying to use knockout to bind the checked attribute like so
<input type="checkbox" data-bind="checked: $root.Value">
My problem is that the values come in as string literals, so instead of getting the value of (true) I get it like this ("true")..which regardless of "true"/"false" it will automatically mark it as true.
I would like to be able to run a simple function that test if it is "true"/"false" and if so return the boolean value back, so knockout will bind properly.
any suggestions?
UPDATE: Data is essentially in the following format
{
"ID": 276,
"Name": "DefaultIsCallToOrder",
"Value": "false",
"Sequence": 7,
"DataType": "checkbox",
},
{
"ID": 277,
"Name": "DefaultIsFeatured",
"Value": "false",
"Sequence": 8,
"DataType": "checkbox",
},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以向视图模型添加计算值,请参阅 jsFiddle:
作为替代方案,您当然可以进行比较直接使用视图中的字符串值,但这会破坏视图与视图模型分离的目的:
You can add a computed value to your view model, see jsFiddle:
As an alternative you can of course compare directly with the string value in your view, that would defeat the purpose of separation of view from view model though:
现在有一种比在视图模型上创建属性更简单的方法,即使用
checkedValue
绑定(在版本 3.0 中添加):来自 http://knockoutjs.com/documentation/checked-binding.html :
There's now a simpler way than creating a property on your view model, using the
checkedValue
binding (added in version 3.0):From http://knockoutjs.com/documentation/checked-binding.html :
如果我理解正确的话,您可以创建一个 JavaScript 函数来调用它来进行比较:
然后将您的输入修改为如下所示:
If I understand you right, you can just create a JavaScript-function that you call to do the comparison:
And then you modify your input to look like this: