jQuery SerializeArray() JSON 字符串
我正在尝试在 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>
I am trying to serializeArray my form submission in jQuery. I am trying to get a JSON like
String or Object. Also if someone can let me know how to pick only those widgets which have a value rather than empty ones that would be perfect.
I was in a hurry hence didnt check the syntax and I apologize for it.
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用:
您可以在这里尝试。
Just use:
You can try it here.
如果您不关心同名的重复表单元素,那么您可以这样做:
在此处使用 下划线。
If you do not care about repetitive form elements with the same name, then you can do:
Using underscore here.