0

我有几个单选按钮组,具体取决于在每个组中选择了哪个单选按钮,该特定答案需要返回 1 个或多个预设在数组中的值。

因此,如果在第 1 组中选择 1,则 A 和 B 值都增加 1,如果选择 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>
4

1 回答 1

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;
}

希望这会给出正确的答案

于 2012-10-16T12:27:30.767 回答