最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Knockout optionsText is encoded - Stack Overflow

programmeradmin2浏览0评论

I am trying to create an html hierarchical select based on this solution and using knockout

However, knockout encodes the string value i return.

How can i decode the text returned from the function?

jsFiddle example

Html:

<select data-bind="options: items, optionsText: getOptionText"></select>

Javascript:

var viewModel = {
    items: ko.observableArray([
        { Text: "Item 1", level: 1 },
        { Text: "Item 2", level: 2 },
        { Text: "Item 3", level: 3 },
        { Text: "Item 4", level: 4 }
    ]),
    getOptionText: function(data) {
        var value = "";
        for (var i = 1; i <= (data.level - 1) * 2; i++) {
            value += "&nbsp;";
        }
        value += data.Text;
        return value;
    }
};
ko.applyBindings(viewModel)

I am trying to create an html hierarchical select based on this solution and using knockout

However, knockout encodes the string value i return.

How can i decode the text returned from the function?

jsFiddle example

Html:

<select data-bind="options: items, optionsText: getOptionText"></select>

Javascript:

var viewModel = {
    items: ko.observableArray([
        { Text: "Item 1", level: 1 },
        { Text: "Item 2", level: 2 },
        { Text: "Item 3", level: 3 },
        { Text: "Item 4", level: 4 }
    ]),
    getOptionText: function(data) {
        var value = "";
        for (var i = 1; i <= (data.level - 1) * 2; i++) {
            value += "&nbsp;";
        }
        value += data.Text;
        return value;
    }
};
ko.applyBindings(viewModel)
Share Improve this question edited May 23, 2017 at 12:22 CommunityBot 11 silver badge asked Jan 21, 2014 at 12:59 CatalinCatalin 11.7k19 gold badges80 silver badges152 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

Replace your "&nbsp;" with "\xA0" (The hexadecimal representation of the Non-Breaking Space character.)

The answer is to use the actual character for a space instead of an HTML entity. To get a non-breaking space type Alt+255. Just do this in place of the &nbsp; in your code. It will look like a normal space but will display as a non-breaking space:

Live example: http://jsfiddle/jLbct/1/

发布评论

评论列表(0)

  1. 暂无评论