knockoutjs:Using built-in bindings:Working with form fields:selectedOptions

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>knockoutjs:Using built-in bindings:Working with form fields:selectedOptions</title>
  6. <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
  7. <script type="text/javascript" src="knockout-2.1.0beta.js"></script>
  8. </head>
  9. <body>
  10. <p>
  11.   Choose some countries you'd like to visit:
  12.   <select data-bind="options: availableCountries, selectedOptions: chosenCountries" size="5" multiple="true"></select>
  13. </p>
  14.  
  15. <script type="text/javascript">
  16.   var viewModel = {
  17.     availableCountries : ko.observableArray(['France', 'Germany', 'Spain']),
  18.     chosenCountries : ko.observableArray(['Germany']) // Initially, only Germany is selected
  19.   };
  20.   // ... then later ...
  21.   // Now France is selected too
  22.   viewModel.chosenCountries.push('France');
  23.   ko.applyBindings(viewModel);
  24. </script>
  25. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  26. <a href="http://tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  27. <a href="http://tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  28. </div>
  29. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  30. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  31. </div>
  32. </body>
  33. </html>