我有 3 组单选按钮,每组包含多个单选按钮。在使用时data-toggle="buttons"
在 bootstrap3 中,对于每一组,我可以确保每组仅选择一个答案。到目前为止一切都很好。
下面的函数在单击事件上触发并返回所选项目的值。我的问题是如何处理存储在数组中的返回值values
并为每个按键指定一个与该组按钮相对应的名称。
假设我选择Red + small + Circle
then getValues
将返回 key=> value :
- [0] => 红色
- [1] => 小
- [2] => 圆圈
现在,如果我选择small + Circle
:
这些值是根据选择的顺序存储的,我试图获取这些值来根据选择构建查询。
我希望以这种格式存储值:
- [颜色] =>x
- [大小] => y
- [形状] => 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
试试这个。
现场演示 http://jsfiddle.net/q7qw5/1/
我认为关联数组在 Javascript 中是可能的。
http://www.i-programmer.info/programming/javascript/1441-javascript-data-structs-the-associative-array.html http://www.i-programmer.info/programming/javascript/1441-javascript-data-structures-the-associative-array.html
<input type="radio" name="color" class="radioGrp" value="red"/> Red
<input type="radio" name="color" class="radioGrp" value="blue"/> Blue
<br/>
<input type="radio" name="size" class="radioGrp" value="small"/> small
<input type="radio" name="size" class="radioGrp" value ="Large"/> Large
<br/>
<input type="radio" name="shape" class="radioGrp" value="circle"/>circle
<input type="radio" name="shape" class="radioGrp" value="square"/>square
<input type='button' id="Btn" />
function getValues(){
var values = [];
var radios = document.getElementsByClassName("radioGrp");
console.log(radios);
for(var x = 0; x < radios.length; x++){
if(radios[x].type == "radio" && radios[x].checked){
values[radios[x].name] = (radios[x].value);
}
}
return values;
}
$("#Btn").click(function() {
var x = getValues();
alert(x["color"]);
alert(x["size"]);
alert(x["shape"]);
for(var prop in x ){
alert( prop + " is " + x[prop] );
}
});
您不能将 $.each 用于关联数组以获取更多详细信息http://bugs.jquery.com/ticket/4319 http://bugs.jquery.com/ticket/4319
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)