我有几个单选按钮组,具体取决于每个组中选择的单选按钮,该特定答案需要返回一个或多个数组中预设的值。
因此,如果在组1中选择1,则A& B值均增加1,如果选择2则B,C& D增量等。最后,应返回具有最多分数/增量的字母(提交表格时)
我怎样才能解决这个问题?我认为起点是返回选定的单选按钮,将它们匹配到正确的数组索引,然后检查该数组中的值(字母),并以最大的增量返回它的值?
$(function(){
var group1 = new Array();
group1[0] = "A, B";
group1[1] = "B, C, D";
group1[2] = "E, B";
group1[3] = "B";
group1[4] = "F";
var group2 = new Array();
group2[0] = "D, A";
group2[1] = "D, C";
group2[2] = "B, F, E";
group2[3] = "A";
group2[4] = "B, D";
$('form').submit(function(){
var checked = $('input:radio:checked').length;
//This is as far as I am
});
});
<form>
<input type="radio" name="group1" value="1" id="q1-1" /> 1
<input type="radio" name="group1" value="2" id="q1-2" /> 2
<input type="radio" name="group1" value="3" id="q1-3" /> 3
<input type="radio" name="group1" value="4" id="q1-4" /> 4
<input type="radio" name="group1" value="5" id="q1-5" /> 5
<br /><br />
<input type="radio" name="group2" value="1" id="q2-1" /> 1
<input type="radio" name="group2" value="2" id="q2-2" /> 2
<input type="radio" name="group2" value="3" id="q2-3" /> 3
<input type="radio" name="group2" value="4" id="q2-4" /> 4
<input type="radio" name="group2" value="5" id="q2-5" /> 5
<input type="submit" value="submit" />
</form>
答案 0 :(得分:0)
检查它会在创建数组时给出结果,所以我用switch来选择数组中的值。
var temp=0;
var result = [];
var maxvalue = 0;
var topQualityjob = "";
$('input:radio:checked').each(function () {
switch ($(this).attr("name")) {
case "group1":
$(group1[parseInt($(this).val()) - 1].split(",")).each(function () {
result.push(this);
temp = getaccurance(result, this);
if (maxvalue < temp) {
maxvalue = temp;
topQualityjob = this;
}
});
break;
case "group2":
$(group2[parseInt($(this).val()) - 1].split(",")).each(function () {
result.push(this);
temp = getaccurance(result, this);
if (maxvalue < temp) {
maxvalue = temp;
topQualityjob = this;
}
});
break;
}
});
alert(topQualityjob);
}
function getaccurance(result, val) {
num = 0;
for (i = 0; i < result.length; i++) {
if (result[i] == val)
num++;
}
return num;
}
希望这会给出正确答案