[問題] JQuery form serialization

作者: shomingchang ( )   2014-03-13 02:54:33
想請問為何 var formData = $('#testform').serialize(); 序列化出來的資料是空的?
感謝大家
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Scripts/jquery-2.1.0.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#btnSerialize').on('click', Serialize);
$('#btnDecode').on('click', Decode);
});
function Serialize() {
var formData = $('#testform').serialize();
$('#beforeDecode').val(formData.toString());
}
function Decode() {
var encodeData = $('#beforeDecode').val();
var decodeData = decodeURIComponent(encodeData);
$('#afterDecode').val(decodeData.toString());
}
</script>
</head>
<body>
<form id="testform" >
<input type="text" id="myName" value="myName"/>
<input type="text" id="myEmail" value="myEmail"/>
</form>
<label for="beforeDecode"> The data of test form is: </label>
<input type="text" id="beforeDecode" />
<label for="afterDecode"> The data of test form is: </label>
<input type="text" id="afterDecode" />
<button id="btnSerialize">Serialize</button>
<button id="btnDecode">Decode</button>
</body>
</html>
作者: mail6543210   2014-03-13 04:41:00
input漏了name??
作者: shomingchang ( )   2014-03-13 04:51:00
可是我這個沒有要submit也需要加 name 嗎?加了就可以了!! 真詭異name for retrieving value,id for operate element?
作者: TonyQ (自立而後立人。)   2014-03-13 13:37:00
因為他本來就是走 form 的 spec ...
作者: mrbigmouth (大嘴先生)   2014-03-13 13:57:00
原本的規格 你form裡面沒加name的input資料就post不出去

Links booklink

Contact Us: admin [ a t ] ucptt.com