我正在尝试在 jQuery 中序列化我的表单提交。我正在尝试获取类似字符串或对象的 JSON。另外,如果有人可以让我知道如何只选择那些有价值的小部件而不是完美的空小部件。
我很着急,因此没有检查语法,对此我深表歉意。
<html>
<head>
<script type="text/javascript">
$(document.ready(function(){
$("#myform").submit(function(){
var mySerialObj = $("#myform").serializeArray();
$.each(mySerialObj,function(indx,idxVal){
//here indx is numeric and idxVal is a String like
// [{{"name","name"},{"value","RED"}}]
$.each(JSON.parse(idxVal),function(i,v){
//here I am not able to get the thinggy into a
// JSON format something like ['name','RED']
});
});
});
});
</script>
</head>
<body>
<form id="myform">
<div>
<span>What color do you prefer?</span><br />
<input type="radio" name="colors" id="red" />Red<br />
<input type="radio" name="colors" id="blue" />Blue<br />
<input type="radio" name="colors" id="green" />Green
</div>
<div>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</div>
</form>
<button type="submit" value="submit" id="sbmt"">submit</button>
</body>
</html>