knockoutjsちゅーとりある:ボタンクリックで大文字変換

  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>knockoutjsちゅーとりある:ボタンクリックで大文字変換</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. <script type="text/javascript">
  9.   $(function () {
  10.     function AppViewModel() {
  11.       this.firstName = ko.observable("Bert");
  12.       this.lastName  = ko.observable("Bertington");
  13.       this.fullName = ko.computed(function() {
  14.         return this.firstName() + " " + this.lastName();
  15.       }, this);
  16.       this.capitalizeLastName = function() {
  17.         var currentVal = this.lastName();
  18.         this.lastName(currentVal.toUpperCase());
  19.       };
  20.     }
  21.     ko.applyBindings(new AppViewModel());
  22.   })();
  23. </script>
  24. </head>
  25. <body>
  26. <p>First name: <input data-bind="value: firstName" /></p>
  27. <p>Last name:  <input data-bind="value: lastName" /></p>
  28. <p>Full name:  <strong data-bind="text: fullName"></strong></p>
  29. <button data-bind="click: capitalizeLastName">Go caps</button>
  30. <p>※「Last name」のみ大文字に変換</p>
  31. <div style="font-size:10pt;text-align:right;margin-top:0.5em;">
  32. <a href="//tips.recatnap.info/" target="_top">PCスキルの小技・忘却防止メモ</a> -
  33. <a href="//tips.recatnap.info/wiki/" target="_top">PCスキルの小技・忘却防止メモのまとめ(wiki)</a>
  34. </div>
  35. <div style="font-size:10pt;text-align:center;margin-top:0.5em;padding:0.5em;border-top:1px solid #ccc;">
  36. Copyright &copy; 2009 by PCスキルの小技・忘却防止メモ. All rights reserved.
  37. </div>
  38. </body>
  39. </html>