0

<option>我想在我的每一个中都有两列的表格<select>
我试过这个JSFIDDLE-source。但是绑定<span>不起作用

<span data-bind="text : countryName"></span>
<span data-bind="text : selectedCountry"></span>  

有任何想法吗?还是另一种解决方案?

编辑
在此处输入图像描述

4

1 回答 1

1

虽然您无法为 a 的内容设置样式,但您可以使用 Twitter 出色的Bootstrap框架之类的东西来获得类似的效果。

http://jsfiddle.net/marrok/QdjPt/

<div class="btn-group">
  <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
    <span class="name" data-bind="text: selectedItem().name"></span>
    <span class="desc" data-bind="text: selectedItem().desc"></span>    <span class="caret"></span>
  </a>

  <ul class="list dropdown-menu" data-bind="foreach: items ">
      <!-- I should be able to use 'click' here but I'm not sure why it doesn't work. -->
      <li class="item" data-bind="event: {mouseover : function(){$root.selectedIndex($index())}}">      
          <span class="name" data-bind="text: name"></span>
          <span class="desc" data-bind="text: desc"></span>
      </li>
  </ul>
</div>

Javascript:

var Thing = function(name, desc) {
    var self = this;
    self.name = ko.observable(name);
    self.desc = ko.observable(desc);
};

var AppModel = function() {
    var self = this;
    self.items = ko.observableArray([
        new Thing(1, "Thing One"),
        new Thing(2, "Thing Two"),
        new Thing(3, "Thing Three"),
        new Thing(4, "Thing Four")]);
    self.selectedIndex = ko.observable(0);
    self.selectedItem = ko.computed(function() {
        return self.items()[self.selectedIndex()];
    })
};

ko.applyBindings(new AppModel());​
于 2012-11-30T21:53:26.783 回答