samedi 23 avril 2016

How to get option value only the selected checkboxes?

I want to get option value of only selected checkbox, but I am getting value from all the checkboxes.

My HTML code:

 <div class= "outer">
    <div class="inner" id="inner">
    <input type="checkbox" name="machine" value="plane">I have a plane<br>
      <select class="weight" name="m2" id="m2">
        <option value="1">Light</option>
        <option value="2">Normal</option>
        <option value="3">Extra</option>
      </select>

    </div>
    <div class="inner" id="inner">
    <input type="checkbox" name="machine" value="plane"> I have 2 planes<br>
       <select class="weight" name="m2" id="m2">
          <option value="1">Light</option>
          <option value="2">Normal</option>
          <option value="3">Extra</option>
       </select>
    </div>
 </div>
<input type="submit" value="Submit">

JS code: function myAction(){ var vals = new Array(); var machine = document.getElementsByName('machine');

   var m2 = document.getElementById('m2');

   for(var i=0, n= machine.length; i<n; i++){
        if(machine[i].checked){
            vals.push(m2.value);
        }
    }
 }




Aucun commentaire:

Enregistrer un commentaire