• AJAX小结二


    表单相关

    表单的组成部分 表单标签 表单域  表单按钮

    表单域:包含了文本框、密码框、多行文本框、复选框、单选框、下拉选择框、和文件上传框等。

    标签的属性

    标签用来采集数据,标签的属性是用来规定 如何把采集到的数据发送到服务器

    属性描述
    actionURL地址规定当提交表单时,向何处发送表单数据
    methodget或post规定以何种方式把表单数据提交到 action URL
    enctype

    application/x-www-form-urlencoded

    multipart/form-data

    text/plain

    规定在发送表单数据之前如何对其进行编码
    target

    _blank

    _self

    _parent

    _top

    framename

    规定在何处打开 action URL

    1.action 

    action属性的值应该是 后端提供的url接口地址 

    未指定属性值的情况下,action的默认值为当前页面的url地址

    当提交表单后 页面会立即跳转到 action属性指定的URL地址

    2.target

    _blank 在新窗口打开

    _self  默认 在相同的框架中打开

    _parent   在父框架集中打开。(很少用)

    _top 在整个窗口中打开。(很少用)

    framename  在指定的框架中打开。(很少用)

    3.method

    默认使用get

    get 方式适合用来提交少量的、简单的数据

    post 方式适合用来提交大量的、复杂的、包含文件上传的数据。

    在实际开发中,表单post方式使用的最多,很少使用get。例如登录、注册、添加数据等表单操作都需要使用post方式来提交表单。

    4.enctype

    application/x-www-form-urlencoded  在发送前编码所有字符(默认)

    multipart/form-data  不对字符编码 在使用包含文件上传控件的表单时,必须使用该值

    text/plain  空格转换为 ‘+’ 号 但不对特殊字符编码(很少用)

    表单的同步提交

    点击类型为 submit 的按钮 触发表单提交的操作,从而使页面跳转action URL 的行为,叫做表单的同步提交。

    同步提交的缺点

    1. 同步提交之后 整个页面会发生跳转,到action url 用户体验很差

    2.同步提交之后,页面之前的状态和数据会丢失

    解决方案 表单只负责采集数据 Ajax负责将数据提交到服务器。

    $.('#form1').submit(function(e){

            e.preventDefault() // 阻止表单的提交和页面的跳转

            alert('监听到了表单的提交事件')

    })

    $('#form2').on('submit',function(e){

            e.preventDefault() // 阻止表单的提交和页面的跳转

            alert('监听到了表单的提交事件')

    })

    serialize() 

    jQuery提供的函数  可以快速获取表单中的数据  但每个表单必须有 name 属性

    $('#form3').serialize()

    // 调用的结果 :  username=用户名的值&password=密码值

    分享一个收集信息提交评论的案例: 

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>06发布评论案例title>
    6. <script src="http://www.wsg3096.com/ass/jquery-3.6.0.js">script>
    7. <link rel="stylesheet" href="http://www.wsg3096.com/ass/initialize.css">
    8. <style>
    9. .box1{
    10. width: 80%;
    11. margin: 40px auto;
    12. }
    13. .box2{
    14. border: 1px solid #2b76ba;
    15. width: 100%;
    16. height: 120px;
    17. border-radius: 8px;
    18. overflow: hidden;
    19. margin-bottom: 40px;
    20. }
    21. .tit{
    22. color: white;
    23. background: #2b76ba;
    24. line-height: 50px;
    25. width: 100%;
    26. font-size: 20px;
    27. padding-left: 16px;
    28. }
    29. .from{
    30. height: 40px;
    31. margin: 12px 20px;
    32. line-height: 40px;
    33. }
    34. .from span{
    35. display: inline-block;
    36. margin-right: 12px;
    37. clear: left;
    38. }
    39. i{
    40. background: #edeeec;
    41. border: 1px solid #d7d9d7;
    42. display: inline-block;
    43. padding: 0px 20px;
    44. border-right: none;
    45. float: left;
    46. height: 38px;
    47. border-radius: 6px 0px 0px 6px;
    48. }
    49. input{
    50. height: 40px;
    51. padding-left: 8px;
    52. border: 1px solid #d7d9d7;
    53. border-left: none;
    54. float: left;
    55. outline: none;
    56. width: 260px;
    57. border-radius: 0px 6px 6px 0px;
    58. }
    59. #sum{
    60. height: 40px;
    61. width: 80px;
    62. background: #1770de;
    63. border-radius: 4px;
    64. color: white;
    65. font-size: 16px;
    66. border: none;
    67. float: right;
    68. cursor: pointer;
    69. }
    70. .comments{
    71. width: 100%;
    72. padding: 16px;
    73. border: 1px solid #e5e5e5;
    74. }
    75. .comments-list{
    76. display: flex;
    77. justify-content: space-between;
    78. align-items: center;
    79. padding: 10px;
    80. border-bottom: 1px solid #e5e5e5;
    81. }
    82. .user{
    83. font-size: 16px;
    84. font-weight: 600;
    85. }
    86. .address{
    87. font-size: 12px;
    88. padding: 4px 6px;
    89. border-radius: 8px;
    90. background: #d1f1ff;
    91. }
    92. .time{
    93. padding: 4px 12px;
    94. border-radius: 8px;
    95. background: #fff6e0;
    96. }
    97. .left2 span{
    98. margin-right: 12px;
    99. }
    100. .comments-text{
    101. margin-left: 30px;
    102. }
    103. style>
    104. head>
    105. <body>
    106. <div class="box1">
    107. <div class="box2">
    108. <div class="tit">添加你的看法div>
    109. <form class="from">
    110. <span>
    111. <i>昵称i>
    112. <input type="text" placeholder="请输入昵称" name="username">
    113. span>
    114. <span>
    115. <i>内容i>
    116. <input type="text" placeholder="请输入评论" name="content">
    117. span>
    118. <span>
    119. <i>地区i>
    120. <input type="text" placeholder="请输入所属地区" >
    121. span>
    122. <input type="submit" value="提交" id="sum">
    123. form>
    124. div>
    125. <div class="comments">
    126. <div class="comments-list">
    127. <div class="left2">
    128. <span class="user">白自在span>
    129. <span class="address">新疆-天山span>
    130. <span class="comments-text">入口柔和、酒香四溢span>
    131. div>
    132. <div class="right2">
    133. <span class="time">2022-05-08 16:40:28span>
    134. div>
    135. div>
    136. div>
    137. div>
    138. <script>
    139. $(function () {
    140. function upData(){
    141. let dataArr = []
    142. $.ajax({
    143. type:'GET',
    144. url:'http://www.liulongbin.top:3006/api/cmtlist',
    145. //data:{id:1},
    146. success:function (res) {
    147. if (res.status !==200) return alert('获取数据失败!')
    148. let data = res.data
    149. for (let i = 10;i>0;i--){
    150. dataArr.push(data[i])
    151. }
    152. let showData = []
    153. $.each(dataArr,function (i,item) {
    154. //console.log(item)
    155. showData.push('
      '+item.username+'河南-郑州'+item.content+'
      '+item.time+'
      '
      )
    156. })
    157. $('.comments').empty().html(showData)
    158. }
    159. })
    160. }
    161. upData()
    162. $('.from').on('submit',function (e) {
    163. e.preventDefault()
    164. let data = $(this).serialize()//获得带有name 的input value
    165. $.post(
    166. 'http://www.liulongbin.top:3006/api/addcmt',
    167. data,
    168. function (res) {
    169. if (res.status !== 201){
    170. console.log('新增评论失败')
    171. }
    172. upData()
    173. $('form')[0].reset()// 重置内容 因为是JS原生方法 把JQ对象转成DOM
    174. }
    175. )
    176. })
    177. })
    178. script>
    179. body>
    180. html>

  • 相关阅读:
    Mac 使用 scp 上传或下载文件/文件夹
    MongoDB的安装
    java计算机毕业设计开放式教学评价管理系统源码+mysql数据库+系统+lw文档+部署
    Unity --- 网格链接与动态障碍物
    Mac M2芯片配置PHP环境
    axios 取消请求:CancelToken
    pdf怎么转换成jpg图片?收藏这几种方法
    设计模式(十一)----结构型模式之装饰者模式
    一、T100应收管理之应收基础数据设置篇
    云开发中关于Container与虚拟机之间的比较
  • 原文地址:https://blog.csdn.net/benlalagang/article/details/126133690