When the below is run, assuming that male is checked, I expect jsonObj to look like this
0: Object
name: "sex"
value: "male_value"
However, it ends up looking like this.
0: Object
name: "sex"
value: "male_value"
1: Object
name: "sex"
value: "male_value"
2: Object
name: "sex"
value: "male_value"
3: Object
name: "sex"
value: "female_value"
HTML
<div id="form">
<div id="radio_buttons">
<input type="radio" name="sex" id="male" value="male_value">Male<br>
<input type="radio" name="sex" id="female" value="female_value">Female
</div>
</div>
JAVASCRIPT
function jsonAdd(name, value)
{
item = {};
item ["name"] = name;
item ["value"] = value;
jsonObj.push(item);
}
jsonObj = [];
$("#form input").each(function() {
//Get Input Type
type = $(this).attr("type");
switch(type)
{
case "radio":
if($(this).prop("checked"))
{
var name = $(this).attr("name");
var value = $(this).val();
jsonAdd(name, value);
}
case "checkbox":
if($(this).prop("checked"))
{
var name = $(this).attr("name");
var value = $(this).val();
jsonAdd(name, value);
}
default:
var name = $(this).attr("name");
var value = $(this).val();
jsonAdd(name, value);
}
});
I can seem to get it working properly if I don't use a switch statement but I don't understand why a simple switch would be causing this issue?
breakin yourswitch. To solve the first problem, one solution is to use FormData