简体   繁体   English

KnockoutJS数据绑定选项

[英]KnockoutJS data-bind optionsCaption

 var vm = { WeatherId: ko.observable(), WeatherConditions: [{ Id: '1', Name: 'Sunny' }, { Id: '2', Name: 'Rainy' }, { Id: '3', Name: 'Cloudy' }, { Id: '4', Name: 'Snowy' }] }; ko.applyBindings(vm); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-min.js"></script> <select data-bind="options: WeatherConditions, value: WeatherId, optionsText:'Name', optionsCaption: 'Select today weather'"> </select> 

I have this KO data bind snippet where I want to set optionsCaption to read as "Select today's weather". 我有这个KO数据绑定代码段,在这里我想将optionsCaption设置为“选择今天的天气”。 I wasn't able to inset the apostrophe in the middle - need help. 我无法在中间插入撇号-需要帮助。

Escape the apostrophe with backslash \\ 用反斜杠转义撇号\\

optionsCaption: 'Select today\\'s weather'">

 var vm = { WeatherId: ko.observable(), WeatherConditions: [{ Id: '1', Name: 'Sunny' }, { Id: '2', Name: 'Rainy' }, { Id: '3', Name: 'Cloudy' }, { Id: '4', Name: 'Snowy' }] }; ko.applyBindings(vm); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-min.js"></script> <select data-bind="options: WeatherConditions, value: WeatherId, optionsText:'Name', optionsCaption: 'Select today\\'s weather'"> </select> 

You just need to escape the ' : today\\'s : 您只需要转义'today\\'s

 var vm = { WeatherId: ko.observable(), WeatherConditions: [{ Id: '1', Name: 'Sunny' }, { Id: '2', Name: 'Rainy' }, { Id: '3', Name: 'Cloudy' }, { Id: '4', Name: 'Snowy' }] }; ko.applyBindings(vm); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.1.0/knockout-min.js"></script> <select data-bind="options: WeatherConditions, value: WeatherId, optionsText:'Name', optionsCaption: 'Select today\\'s weather'"> </select> 

This isn't a KnockoutJS thing, it's a basic JavaScript thing. 这不是KnockoutJS,而是基本的JavaScript。 To put a ' in a ' -quoted string, you use an escape: today\\'s . 要将'放在'引号中的字符串中,请使用转义符: today\\'s

Remember, the content of the data-bind attribute is an JavaScript object initializer without the { and } , whose contents are evaluated within a series of with blocks to provide context. 请记住, data-bind属性的内容是不带{}的JavaScript对象初始化程序,其内容在一系列with块中进行求值以提供上下文。 (Literally. Knockout builds the text of a function to do that, using as many with blocks as it needs for the nesting level, and with a return {" + theDataBindAttributeText}; at the end, then uses new Function to build a function from that, and calls it to get the bindings.) (从字面上看。Knockout生成函数的文本来执行此操作,在嵌套级别使用所需数量的with块代码,并return {" + theDataBindAttributeText};最后,然后使用new Function从然后调用它来获取绑定。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM