gpt4 book ai didi

jQuery SerializeArray() JSON 字符串

转载 作者:行者123 更新时间:2023-12-01 08:23:56 29 4
gpt4 key购买 nike

我正在尝试在 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>

最佳答案

只需使用:

$("#myform").serialize();

You can try it here.

关于jQuery SerializeArray() JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5467206/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com