html:

<h2>Your seat reservations</h2>

<table>
    <thead>

    <tr>
           <th>Passenger name</th>

      <th>Meal</th>

      <th>Surcharge</th>

      <th></th>
      </tr>

  </thead>
    <tbody data-bind="foreach: seats">
        <tr>
            <td data-bind="text: name"></td>
            <td data-bind="text: meal().mealName"></td>
            <td data-bind="text: meal().price"></td>
        </tr>    
    </tbody>
</table>
<select data-bind="foreach: seats">
    <option data-bind="text: name;value:text: meal().price"></option>
</select>

 

 

 

js代码:

// 一个构造函数
function SeatReservation(nameinitialMeal{
    var self this;
    self.name name;
    self.meal ko.observable(initialMeal);
}

//数据模型
function ReservationsViewModel({
    var self this;

    //数据
    self.availableMeals [
        mealName"Standard (sandwich)"price},
        mealName"Premium (lobster)"price34.95 },
        mealName"Ultimate (whole zebra)"price290 }
    ];    

    // 监控数组
    self.seats ko.observableArray([
        new SeatReservation("Steve"self.availableMeals[0]),
        new SeatReservation("Bert"self.availableMeals[1])
    ]);
}
//传递数据
ko.applyBindings(new ReservationsViewModel());

 

效果页:

Your seat reservations

Passenger name Meal Surcharge  
Steve Standard (sandwich) 0
Bert Premium (lobster) 34.95

相关文章:

  • 2022-12-23
  • 2021-08-14
  • 2022-12-23
  • 2022-02-20
  • 2021-09-13
  • 2022-12-23
  • 2021-06-25
  • 2021-09-09
猜你喜欢
  • 2022-12-23
  • 2022-02-23
  • 2022-01-03
  • 2021-11-29
  • 2021-08-05
  • 2021-05-20
  • 2022-12-23
相关资源
相似解决方案