博客列表 >表单事件、键盘事件、get

表单事件、键盘事件、get

橙絮圆
橙絮圆原创
2021年07月28日 10:33:11446浏览

表单事件、键盘事件、get

作业标题:0722作业
作业内容:1、演示表单事件 2、演示键盘事件 3、演示 get


  • 表单事件

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>事件</title>
    8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    9. <style>
    10. * {
    11. background-color: #d4edda;
    12. text-align: center;
    13. font-size: 20px;
    14. }
    15. .form-control {
    16. width: 500px;
    17. padding: 0.375rem 0.75rem;
    18. font-size: 1rem;
    19. font-weight: 400;
    20. line-height: 1.5;
    21. color: #495057;
    22. background-color: #fff;
    23. background-clip: padding-box;
    24. border: 1px solid #ced4da;
    25. border-radius: 0.25rem;
    26. transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
    27. }
    28. button {
    29. width: 600px;
    30. color: #fff;
    31. background-color: #28a745;
    32. border-color: #28a745;
    33. }
    34. .select {
    35. width: 265px;
    36. height: 47px;
    37. }
    38. </style>
    39. </head>
    40. <body>
    41. <h2 class="title">注册</h2>
    42. <form action="" onsubmit="return false;">
    43. <p>
    44. 账户:
    45. <input type="text" class="form-control" id="account" />
    46. <div id="ac" style="color:red;font-size:12px;display: none;">请输入账户,账户必须大于8位</div>
    47. </p>
    48. <p>
    49. 密码:
    50. <input type="password" class="form-control" id="password" />
    51. <div id="pw" style="color:red;font-size:12px;display: none;">请输入密码</div>
    52. </p>
    53. <p>
    54. 省市:
    55. <select id="prov" class="form-control select">
    56. <option value="">请选择</option>
    57. <option value="1">安徽</option>
    58. <option value="2">江苏</option>
    59. <option value="3">河南</option>
    60. </select>
    61. <select id="city" class="form-control select">
    62. </select>
    63. </p>
    64. <button>注册</button>
    65. <div id="ac_input" style="margin-top:20px;color:red;font-size: 18px;display: none;"></div>
    66. </form>
    67. </body>
    68. <script>
    69. //表单事件
    70. //1、submit 当提交表单时,会触发的事件
    71. // $("form").submit(function(){
    72. // let account=$("#account").val();
    73. // alert(account);
    74. // });
    75. // $("button").click(function(){
    76. // let account=$("#account").val();
    77. // alert(account);
    78. // })
    79. // 2、change 当select元素的值发生变化时,触发的事件
    80. $("#prov").change(function(){
    81. let prov=$("#prov").val();
    82. let html='<option value="">请选择<option>';
    83. if(prov==1){
    84. html+='<option value="11">合肥</option>';
    85. html+='<option value="12">淮南</option>';
    86. html+='<option value="13">蛙埠</option>';
    87. html+='<option value="14">芜湖</option>';
    88. }
    89. $("#city").html(html);
    90. });
    91. //3、focus 当元素获得焦点时,触发事件
    92. $("#account").focus(function(){
    93. $("#ac").show();
    94. });
    95. $("#account").focus(function(){
    96. $("#pw").show();
    97. });
    98. //4、blur元素失去焦点的时候触发事件
    99. //
    100. $("#account").blur(function(){
    101. let account=$("#account").val();
    102. console.log(account);
    103. if(!account){
    104. $(this).focus();
    105. }else{
    106. $("#ac").hide();
    107. }
    108. })
    109. $("#account").blur(function(){
    110. $("#pw").show();
    111. });
    112. </script>
    113. </html>
  • 演示键盘事件

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>事件</title>
    8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    9. <style>
    10. * {
    11. background-color: #d4edda;
    12. text-align: center;
    13. font-size: 20px;
    14. }
    15. .form-control {
    16. width: 500px;
    17. padding: 0.375rem 0.75rem;
    18. font-size: 1rem;
    19. font-weight: 400;
    20. line-height: 1.5;
    21. color: #495057;
    22. background-color: #fff;
    23. background-clip: padding-box;
    24. border: 1px solid #ced4da;
    25. border-radius: 0.25rem;
    26. transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
    27. }
    28. button {
    29. width: 600px;
    30. color: #fff;
    31. background-color: #28a745;
    32. border-color: #28a745;
    33. }
    34. .select {
    35. width: 265px;
    36. height: 47px;
    37. }
    38. </style>
    39. </head>
    40. <body>
    41. <h2 class="title">注册</h2>
    42. <form action="" onsubmit="return false;">
    43. <p>
    44. 账户:
    45. <input type="text" class="form-control" id="account" />
    46. <div id="ac" style="color:red;font-size:12px;display: none;">请输入账户,账户必须大于8位</div>
    47. </p>
    48. <p>
    49. 密码:
    50. <input type="password" class="form-control" id="password" />
    51. <div id="pw" style="color:red;font-size:12px;display: none;">请输入密码</div>
    52. </p>
    53. <p>
    54. 省市:
    55. <select id="prov" class="form-control select">
    56. <option value="">请选择</option>
    57. <option value="1">安徽</option>
    58. <option value="2">江苏</option>
    59. <option value="3">河南</option>
    60. </select>
    61. <select id="city" class="form-control select">
    62. <option value="">请选择</option>
    63. </select>
    64. </p>
    65. <button>注册</button>
    66. <div id="ac_input" style="margin-top:20px;color:red;font-size: 18px;display: none;"></div>
    67. </form>
    68. </body>
    69. <script>
    70. $("form").submit(function(){
    71. let account=$("#account").val();
    72. if(!account){
    73. msg('请输入账户');
    74. return false;
    75. }
    76. if(account=='admin' || account=='phpcn'){
    77. msg('账户已存在');
    78. return false;
    79. }
    80. let password=$("#password").val();
    81. if(!password){
    82. msg('请输入密码');
    83. return false;
    84. }
    85. if(password.length<6){
    86. msg('请输入大于6位的密码');
    87. return false;
    88. }
    89. alert("注册成功");
    90. })
    91. function msg(data){
    92. $("#ac_input").text(data);
    93. $("#ac_input").show();
    94. }
    95. $("#account").keydown(function(){
    96. $("#ac_input").hide();
    97. })
    98. $("#password").keydown(function(){
    99. $("#ac_input").hide();
    100. })
    101. </script>
    102. </html>
  • 演示 get
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>事件</title>
    8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    9. <style>
    10. * {
    11. text-align: center;
    12. font-size: 20px;
    13. }
    14. .title {
    15. text-align: center;
    16. }
    17. .width {
    18. width: 1200px;
    19. }
    20. tr {
    21. height: 50px;
    22. }
    23. </style>
    24. </head>
    25. <body>
    26. <h2 class="title">省份表</h2>
    27. <table class="width" id="tableId" border="1" align="center" cellspacing="0">
    28. <thead>
    29. <tr>
    30. <th>编号</th>
    31. <th>名称</th>
    32. <th>首字母</th>
    33. <th>拼音</th>
    34. <th>精度</th>
    35. <th>维度</th>
    36. </tr>
    37. </thead>
    38. <tbody></tbody>
    39. </table>
    40. </body>
    41. <script>
    42. // 请求服务器上的省列表
    43. // url:http://admin.ouyangke.cn/index.php/api/index/prov_list
    44. //参数:
    45. //page 当前页数,默认第一页
    46. //limit 一页有多少条,默认10条
    47. //fields
    48. //order
    49. $.get(
    50. "http://admin.ouyangke.cn/index.php/api/index/prov_list",
    51. { limit: 5, page: 3 },
    52. function (data, status) {
    53. console.log(data.data);
    54. let data_html = " ";
    55. for (let i = 0; i < data.data.length; i++) {
    56. console.log("aaa");
    57. data_html += "<tr>";
    58. data_html += "<th>" + data.data[i].area_id + "</th>";
    59. data_html += "<td>" + data.data[i].area_name + "</td>";
    60. data_html += "<td>" + data.data[i].first_pinyin + "</td>";
    61. data_html += "<td>" + data.data[i].pinyin + "</td>";
    62. data_html += "<td>" + data.data[i].lng + "</td>";
    63. data_html += "<td>" + data.data[i].lat + "</td>";
    64. data_html += "</tr>";
    65. }
    66. $("tbody").append(data_html);
    67. },
    68. "json"
    69. );
    70. </script>
    71. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议