jQuery.serialize()方法在jQuery中做什么?
该serialize()方法将一组输入元素序列化为数据字符串。假设我们在serialize.php文件中具有以下PHP内容-
<?php
if( $_REQUEST["name"] ) {
$name = $_REQUEST['name'];
echo "Welcome ". $name;
$age = $_REQUEST['age'];
echo "<br />Your age : ". $age;
$sex = $_REQUEST['sex'];
echo "<br />Your gender : ". $sex;
}
?>示例
以下是显示此方法用法的代码片段-
<head>
<script src = "https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#driver").click(function(event){
$.post(
"/jquery/serialize.php",
$("#testform").serialize(),
function(data) {
$('#stage1').html(data);
}
);
var str = $("#testform").serialize();
$("#stage2").text(str);
});
});
</script>
</head>
<body>
<p>Click on the button to load result.html file:</p>
<div id = "stage1" style = "background-color:blue;">
STAGE - 1
</div>
<br />
<div id = "stage2" style = "background-color:blue;">
STAGE - 2
</div>
<form id = "testform">
<table>
<tr>
<td><p>Name:</p></td>
<td><input type = "text" name = "name" size = "40" /></td>
</tr>
<tr>
<td><p>Age:</p></td>
<td><input type = "text" name = "age" size = "40" /></td>
</tr>
<tr>
<td><p>Sex:</p></td>
<td> <select name = "sex">
<option value = "Male" selected>Male</option>
<option value = "Female" selected>Female</option>
</select></td>
</tr>
<tr>
<td colspan = "2">
<input type = "button" id = "driver" value = "Load Data" />
</td>
</tr>
</table>
</form>
</body>热门推荐
4 圆圆的祝福语简短
10 双节祝福语简短唯美
11 亲戚生日红包祝福语简短
12 祝福语大全简短6个
13 晚儿子生日祝福语简短
14 3岁孩子祝福语简短
15 团队文案祝福语简短励志
16 早安祝福语大全简短励志
17 班级集体生日祝福语简短
18 女儿生日祝福语说说简短