js进阶 14-7 jquery的ajax部分为什么需要对表单进行序列化

js进阶 14-7 jquery的ajax部分为什么需要对表单进行序列化

一、总结

一句话总结:如果用ajax传递表单的数据,如果不进行表单的序列化,要一个参数一个参数的写,太麻烦,序列化的话,一句代码搞定。data:$('form').serialize(),这样一句话解决复杂的表单ajax的post传值过程。

 

1、表单序列化函数是什么?

$(selector).serialize()和serializeArray()

24     <script>
25  $(function(){ 26  $('form input[type=button]').click(function(){ 27  $.ajax({ 28  type:'POST', 29  url:'buy.php', 30 // data:{ 31 // user:$('form input[name=user]').val(), 32 // Tel:$('form input[name=Tel]').val(), 33 // buy:$('form select[name=buy]').val() 34 // }, 35  data:$('form').serialize(), 36  success:function(responseTxt,statusTxt,xhr){ 37 //alert(responseTxt) 38  $('#txt').html(responseTxt) 39  } 40  }) 41 42  }) 43  }) 44 </script>

 

2、表单序列化函数serialize()如何使用?

$(selector).serialize(),其实设置好监听对象就好了

35                     data:$('form').serialize(),

 

3、表单序列化的优势是什么(讲解+实例)?

极大的减少代码量和出错

jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,这样,我们就可以把序列化的值传给ajax()作为url的参数,轻松使用ajax()提交form表单了,而不需要一个一个获取表单中的值然后传给ajax()

24     <script>
25  $(function(){ 26  $('form input[type=button]').click(function(){ 27  $.ajax({ 28  type:'POST', 29  url:'buy.php', 30 // data:{ 31 // user:$('form input[name=user]').val(), 32 // Tel:$('form input[name=Tel]').val(), 33 // buy:$('form select[name=buy]').val() 34 // }, 35  data:$('form').serialize(), 36  success:function(responseTxt,statusTxt,xhr){ 37 //alert(responseTxt) 38  $('#txt').html(responseTxt) 39  } 40  }) 41 42  }) 43  }) 44 </script>

 

 

 

二、jquery的ajax部分为什么需要对表单进行序列化

1、相关知识

表单序列化

  • 语法:$(selector).serialize()

    jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,这样,我们就可以把序列化的值传给ajax()作为url的参数,轻松使用ajax()提交form表单了,而不需要一个一个获取表单中的值然后传给ajax()

  • serializeArray()序列化表格元素(类似'.serialize()'方法返回JSON数据结构数据。

    ’’’注意’’’此方法返回的是JSON对象而非JSON字符串。

 

 

2、代码

 

html
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <style>
 4 </style>
 5 <head>
 6     <meta charset="UTF-8">
 7     <title>演示文档</title>
 8     <script type="text/javascript" src="jquery-3.1.1.min.js"></script>
 9     <style type="text/css">
10       </style>
11 </style>
12 </head>
13 <body>
14     <form id="form1">
15     姓名:<input type="text" name="user"><br>
16     电话:<input type="text" name="Tel"><br>
17           <select name="buy">
18             <option>买新房</option>
19             <option>看二手房</option>
20           </select>
21           <input type="button" value="提交">
22     </form>
23     <div id="txt"></div>
24     <script>
25         $(function(){
26             $('form input[type=button]').click(function(){
27                 $.ajax({
28                     type:'POST',
29                     url:'buy.php',
30                     // data:{
31                     //     user:$('form input[name=user]').val(),
32                     //     Tel:$('form input[name=Tel]').val(),
33      //                      buy:$('form select[name=buy]').val()
34                     // },
35                     data:$('form').serialize(),
36                     success:function(responseTxt,statusTxt,xhr){
37                         //alert(responseTxt)
38                         $('#txt').html(responseTxt)
39                     }
40                 })
41 
42             })
43         })
44     </script>
45 </body>
46 </html>

 

 

php
<?php
    echo $_POST['buy'].'---'.$_POST['user'].'---'.$_POST['Tel']
?>

 

 

 

 

 

 

转载于:https://www.cnblogs.com/Renyi-Fan/p/9340689.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值