ajax.tpl 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. mple</title>
  2. <script type="text/javascript"
  3. src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  4. <script type="text/javascript">
  5. var $SCRIPT_ROOT = "{{ request.script_name }}";
  6. </script>
  7. <script type="text/javascript">
  8. $(function() {
  9. var submit_form = function(e) {
  10. $.getJSON($SCRIPT_ROOT + '_add_numbers', {
  11. a: $('input[name="a"]').val(),
  12. b: $('input[name="b"]').val()
  13. }, function(data) {
  14. $('#result').text(data.result);
  15. $('input[name=a]').focus().select();
  16. });
  17. return false;
  18. };
  19. $('a#calculate').bind('click', submit_form);
  20. $('input[type=text]').bind('keydown', function(e) {
  21. if (e.keyCode == 13) {
  22. submit_form(e);
  23. }
  24. });
  25. $('input[name=a]').focus();
  26. });
  27. </script>
  28. <h1>jQuery Example</h1>
  29. <p>
  30. <input type="text" size="5" name="a"> +
  31. <input type="text" size="5" name="b"> =
  32. <span id="result">?</span>
  33. <p><a href=# id="calculate">calculate server side</a>
  34. </body>
  35. </html>