Jquery-ajax-serialize
提供:Dev Guides
jQuery-serialize()メソッド
説明
- serialize()*メソッドは、一連の入力要素をデータの文字列にシリアル化します。
構文
このメソッドを使用するための簡単な構文は次のとおりです-
$.serialize( )
パラメーター
これは、この方法で使用されるすべてのパラメータの説明です-
- NA
例
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;
}
?>
以下は、このメソッドの使用方法を示す簡単な例です-
<html>
<head>
<title>The jQuery Example</title>
<script type = "text/javascript"
src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$("#driver").click(function(event){
$.post(
"/jquery/serialize.php",
$("#testform").serialize(),
function(data) {
$('#stage1')l(data);
}
);
var str = $("#testform").serialize();
$("#stage2").text(str);
});
});
</script>
</head>
<body>
<p>Click on the button to load resultl 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>
</html>
これにより、次の結果が生成されます–