JS How to determine which users choose which option form?

JS How to determine which users choose which option form?

HTML code:

<form name="form1" onsubmit="return foo();">

    A<input type="radio" name="radioGroup" value="a" />

    B<input type="radio" name="radioGroup" value="b" />

    C<input type="radio" name="radioGroup"   />

    D<input type="radio" name="radioGroup" />

    E<input type="radio" name="radioGroup" />

    F<input type="radio" name="radioGroup" />

    <input type="submit" />

  </form>

Method a: a collection of elements into an array example, determined according to the current element in the array element corresponding to the index position.

    var aInput = document.getElementsByTagName("input");
    var arry = [];
    var nowIndex = null;
    for (var i = 0; i < aInput.length-1; i++) {
      arry[i] = aInput[i];
      aInput[i].onclick = function () {
        nowIndex = arry.indexOf(this);
        console.log("点击了第" + nowIndex + "个选项。");
      }
    }
    function foo() {
      alert(nowIndex+1);
    }

Method two: input value by the value of the element to determine the element index clicked, this method is not as good as the first defect a method flexible, less portable.

    function foo() {
      alert(nowIndex+1)
    }
    var aInput = document.getElementsByTagName("input");
    var arry = [];
    var nowIndex = null;
    for (var i = 0; i < aInput.length-1; i++) {

      aInput[i].onclick = function () {

        var optValue = this["value"];
        switch (optValue) {
          case "a":
            nowIndex = 0;
            break;
          case "b":
            nowIndex = 1;
            break;
          case "c":
            nowIndex = 2;
            break;
          case "d":
            nowIndex = 3;
            break;
          case "e":
            =. 4 nowIndex; 
            BREAK; 
          Case "F": 
            nowIndex =. 5; 
            BREAK; 
          default: 
            nowIndex = null; 
            ; 

        } 
        the console.log; ( "clicked on" + nowIndex + "options.") 
      } 
    }

  

Guess you like

Origin www.cnblogs.com/f6056/p/11988518.html