I have 3 groups of radio buttons each containing multiple ones. While using the data-toggle="buttons"
in bootstrap3 for each group I can make sure, only one answer is selected per group. So far everything is fine.
The function below is triggered on a click event and returns the values of the selected items. My problem is how can I handle the returning values stored in the array values
and give each key a name corresponding to the group set of buttons.
Assuming I select Red + small + Circle
then getValues
will return the key=> values :
- [0] => Red
- [1] => Small
- [2] => Circle
Now, if I select small + Circle
:
- [0] => Small
- [1]=> Circle
The values are stored as per the order of selection, Im trying to get this values to build a query as per the selection..
Where I want the values to be store in this format :
- [color] =>x
- [size] => y
- [shape] => z
d
<input type="radio" name="color" value="red"/> Red /// Grp 1 Color
<input type="radio" name="color" value="blue"/> Blue
<br/>
<input type="radio" name="size" value="small"/> small /// Grp2 Size
<input type="radio" name="size" value ="Large"/> Large
<br/>
<input type="radio" name="shape" value="circle"/> Circle /// Grp3 Shape
<input type="radio" name="shape" value="square"/> Square
function getValues(){
var values = [];
for(var x = 0; x < radios.length; x++){
if(radios[x].type == "radio" && radios[x].checked){
values.push(radios[x].value);
}
}
return values;
}
$( "#Btn" ).click(function() {
var x = getValues();
$.each(x, function(k, v) {
//display the key and value pair
alert(k + ' is ' + v);
});
Thanks
color:red
,size:small
等,而不仅仅是red
,small
等。然后通过处理所得到的内容来轻松拆分并拥有自己的键。 - techfoobar