<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD"><HTML><Head> <Scripttype= "Text/javascript"src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></Script> <Scripttype= "Text/javascript"src= "Jslib/jquery.tmpl.min.js"></Script> <Scripttype= "Text/javascript"src= "Jslib/knockout-2.2.0.js"></Script></Head><Body><H3>Meal upgrades</H3><P>Make your flight more bearable by selecting a meal to match your social and economic status.</P>Chosen Meal:<SelectData-bind= "Options:availablemeals, Optionstext: ' Mealname ', value:chosenmeal"></Select><P>You ' ve chosen:<bData-bind= "text:chosenmeal (). description"></b>(Price:<spanData-bind= ' Text:Formatprice (Chosenmeal (). Extracost) '></span>)</P><Scripttype= "Text/javascript"> varavailablemeals=[{mealname:' Standard', Description:'Dry crusts of bread', Extracost:0}, {mealname:'Premium', Description:'Fresh bread with cheese', Extracost:9.95}, {mealname:'Deluxe', Description:'Caviar and vintage Dr Pepper', Extracost:18.50 } ]; varViewModel={chosenMeal:ko.observable (availablemeals[0]) }; Ko.applybindings (ViewModel); functionFormatprice (price) {return Price== 0 ? " Free" : "$" +price.tofixed (2); }</Script></Body></HTML>
1. You can directly bind a variable other than ViewModel.
The JavaScript method can be called directly within the 2.data-bind tag.
Learn Uncle Tom's Knockout tutorial notes