[问题] 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 (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