我正在使用Jquery使用一种单选按钮和数组,但是我无法将所选值记录到一个数组中。这是我的代码:
<script>
$(document).ready(function(){
$("#submit").click(function(){
var x = $("form").serializeArray();
$.each(x, function(i, field){
//do other stuff with it later
});
});
});
</script>
<form>
<fieldset id="group1">
<div>
<input type="radio" id="r1" name="group1"" value="STEM">
<label for="r1"> STEM (Science, Tech, Engineering and Math)</label>
</div>
<div>
<input type="radio" id="r2" name="group1"" value="HUMAN">
<label for="r2">Humanities (Art, History, Music, Language) </label>
</div>
</fieldset>
<fieldset id="group2">
<div>
<input type="radio" id="r3" name="group2" value="EXTRO">
<label for="r3"> Extrovert (Like being around other people)</label>
</div>
<div>
<input type="radio" id="r4" name="group2" value="INTRO">
<label for="r4">Introvert (Don't like being around other people)</label>
</div>
</fieldset>
</form>
<button id="submit" type="button" value="Submit">Submit</button>
我想输出到一个数组,但是目前它输出到每个字段集的数组中,我该如何解决?
侨
答案 0 :(得分:4)
也许我没有正确理解这个问题,但似乎函数的输出是一个对象数组,[{name:&#39; group1&#39;,value:...},{名称:&#39; group2&#39;,值:...}] 因此,如果您只想在一个数组中使用这些值,那么这似乎就可以完成这项工作。
var x = $("form").serializeArray().map((ob) => ob.value);
(我想询问详细信息,但我不能发表评论,因为我没有足够的声誉......希望它有所帮助。)