课程表

jQuery UI 基础

jQuery UI 主题

jQuery UI 部件库

jQuery UI 参考手册

jQuery UI 实例

工具箱
速查手册

旋转器(Spinner)

当前位置:免费教程 » JS/JS库/框架 » jQuery UI

通过向上/向下按钮和箭头键处理,为输入数值增强文本输入功能。

如需了解更多有关 spinner 部件的细节,请查看 API 文档 旋转器部件(Spinner Widget)

默认功能

默认的旋转器。

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 旋转器(Spinner) - 默认功能</title>
  6. <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7. <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8. <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
  9. <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  10. <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  11. <script>
  12. $(function() {
  13. var spinner = $( "#spinner" ).spinner();
  14. $( "#disable" ).click(function() {
  15. if ( spinner.spinner( "option", "disabled" ) ) {
  16. spinner.spinner( "enable" );
  17. } else {
  18. spinner.spinner( "disable" );
  19. }
  20. });
  21. $( "#destroy" ).click(function() {
  22. if ( spinner.data( "ui-spinner" ) ) {
  23. spinner.spinner( "destroy" );
  24. } else {
  25. spinner.spinner();
  26. }
  27. });
  28. $( "#getvalue" ).click(function() {
  29. alert( spinner.spinner( "value" ) );
  30. });
  31. $( "#setvalue" ).click(function() {
  32. spinner.spinner( "value", 5 );
  33. });
  34. $( "button" ).button();
  35. });
  36. </script>
  37. </head>
  38. <body>
  39. <p>
  40. <label for="spinner">选择一个值:</label>
  41. <input id="spinner" name="value">
  42. </p>
  43. <p>
  44. <button id="disable">切换禁用/启用</button>
  45. <button id="destroy">切换部件</button>
  46. </p>
  47. <p>
  48. <button id="getvalue">获取值</button>
  49. <button id="setvalue">设置值为 5</button>
  50. </p>
  51. </body>
  52. </html>

我要试一下


货币

本实例是一个捐款表格,带有货币选择和数量旋转器。

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 旋转器(Spinner) - 货币</title>
  6. <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7. <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8. <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
  9. <script src="/static/js/jqueryui/resources/demos/external/globalize.js"></script>
  10. <script src="/static/js/jqueryui/resources/demos/external/globalize.culture.de-DE.js"></script>
  11. <script src="/static/js/jqueryui/resources/demos/external/globalize.culture.ja-JP.js"></script>
  12. <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  13. <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  14. <script>
  15. $(function() {
  16. $( "#currency" ).change(function() {
  17. $( "#spinner" ).spinner( "option", "culture", $( this ).val() );
  18. });
  19. $( "#spinner" ).spinner({
  20. min: 5,
  21. max: 2500,
  22. step: 25,
  23. start: 1000,
  24. numberFormat: "C"
  25. });
  26. });
  27. </script>
  28. </head>
  29. <body>
  30. <p>
  31. <label for="currency">要捐款的货币</label>
  32. <select id="currency" name="currency">
  33. <option value="en-US">US $</option>
  34. <option value="de-DE">EUR €</option>
  35. <option value="ja-JP">YEN ¥</option>
  36. </select>
  37. </p>
  38. <p>
  39. <label for="spinner">要捐款的数量:</label>
  40. <input id="spinner" name="spinner" value="5">
  41. </p>
  42. </body>
  43. </html>

我要试一下


小数

本实例是一个小数旋转器。增量设置为 0.01。处理文化变化的代码会读取当前的选择器的值,当改变文化时,会基于新的文化重新设置值的样式。

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 旋转器(Spinner) - 小数</title>
  6. <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7. <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8. <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
  9. <script src="/static/js/jqueryui/resources/demos/external/globalize.js"></script>
  10. <script src="/static/js/jqueryui/resources/demos/external/globalize.culture.de-DE.js"></script>
  11. <script src="/static/js/jqueryui/resources/demos/external/globalize.culture.ja-JP.js"></script>
  12. <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  13. <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  14. <script>
  15. $(function() {
  16. $( "#spinner" ).spinner({
  17. step: 0.01,
  18. numberFormat: "n"
  19. });
  20. $( "#culture" ).change(function() {
  21. var current = $( "#spinner" ).spinner( "value" );
  22. Globalize.culture( $(this).val() );
  23. $( "#spinner" ).spinner( "value", current );
  24. });
  25. });
  26. </script>
  27. </head>
  28. <body>
  29. <p>
  30. <label for="spinner">小数旋转器:</label>
  31. <input id="spinner" name="spinner" value="5.06">
  32. </p>
  33. <p>
  34. <label for="culture">选择一种用于格式化的文化:</label>
  35. <select id="culture">
  36. <option value="en-EN" selected="selected">English</option>
  37. <option value="de-DE">German</option>
  38. <option value="ja-JP">Japanese</option>
  39. </select>
  40. </p>
  41. </body>
  42. </html>

我要试一下


地图

谷歌地图集成,使用旋转器来改变纬度和经度。

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 旋转器(Spinner) - 地图</title>
  6. <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7. <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
  8. <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  9. <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
  10. <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  11. <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  12. <script>
  13. $(function() {
  14. function latlong() {
  15. return new google.maps.LatLng( $("#lat").val(), $("#lng").val() );
  16. }
  17. function position() {
  18. map.setCenter( latlong() );
  19. }
  20. $( "#lat, #lng" ).spinner({
  21. step: .001,
  22. change: position,
  23. stop: position
  24. });
  25. var map = new google.maps.Map( $("#map")[0], {
  26. zoom: 8,
  27. center: latlong(),
  28. mapTypeId: google.maps.MapTypeId.ROADMAP
  29. });
  30. });
  31. </script>
  32. <style>
  33. #map {
  34. width:500px;
  35. height:500px;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <label for="lat">纬度</label>
  41. <input id="lat" name="lat" value="44.797">
  42. <br>
  43. <label for="lng">经度</label>
  44. <input id="lng" name="lng" value="-93.278">
  45. <div id="map"></div>
  46. </body>
  47. </html>

我要试一下


溢出

溢出旋转器限制范围从 -10 到 10。对于 10 以上的值,会溢出到 -10,反之亦然。

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 旋转器(Spinner) - 溢出</title>
  6. <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7. <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8. <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
  9. <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  10. <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  11. <script>
  12. $(function() {
  13. $( "#spinner" ).spinner({
  14. spin: function( event, ui ) {
  15. if ( ui.value > 10 ) {
  16. $( this ).spinner( "value", -10 );
  17. return false;
  18. } else if ( ui.value < -10 ) {
  19. $( this ).spinner( "value", 10 );
  20. return false;
  21. }
  22. }
  23. });
  24. });
  25. </script>
  26. </head>
  27. <body>
  28. <p>
  29. <label for="spinner">选择一个值:</label>
  30. <input id="spinner" name="value">
  31. </p>
  32. </body>
  33. </html>

我要试一下


时间

一个扩展自旋转器的自定义部件。使用 全球化(Globalization)插件来解析和输出时间戳,带有自定义的 step 和 page 选项。向上/向下光标用于分钟的递增/递减,向上/向下翻页用于小时的递增/递减。

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 旋转器(Spinner) - 时间</title>
  6. <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7. <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8. <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
  9. <script src="/static/js/jqueryui/resources/demos/external/globalize.js"></script>
  10. <script src="/static/js/jqueryui/resources/demos/external/globalize.culture.de-DE.js"></script>
  11. <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  12. <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  13. <script>
  14. $.widget( "ui.timespinner", $.ui.spinner, {
  15. options: {
  16. // 秒
  17. step: 60 * 1000,
  18. // 小时
  19. page: 60
  20. },
  21. _parse: function( value ) {
  22. if ( typeof value === "string" ) {
  23. // 已经是一个时间戳
  24. if ( Number( value ) == value ) {
  25. return Number( value );
  26. }
  27. return +Globalize.parseDate( value );
  28. }
  29. return value;
  30. },
  31. _format: function( value ) {
  32. return Globalize.format( new Date(value), "t" );
  33. }
  34. });
  35. $(function() {
  36. $( "#spinner" ).timespinner();
  37. $( "#culture" ).change(function() {
  38. var current = $( "#spinner" ).timespinner( "value" );
  39. Globalize.culture( $(this).val() );
  40. $( "#spinner" ).timespinner( "value", current );
  41. });
  42. });
  43. </script>
  44. </head>
  45. <body>
  46. <p>
  47. <label for="spinner">时间旋转器:</label>
  48. <input id="spinner" name="spinner" value="08:30 PM">
  49. </p>
  50. <p>
  51. <label for="culture">选择一种用于格式化的文化:</label>
  52. <select id="culture">
  53. <option value="en-EN" selected="selected">English</option>
  54. <option value="de-DE">German</option>
  55. </select>
  56. </p>
  57. </body>
  58. </html>

我要试一下

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号