Input with multiple HTML values

1

Well, my question is the following would it be possible for me to have a radio input with for example the values:

Portugal Spain France Brazil, Germany

And the person can select 2 options or later when it is passed to PHP had 2 values?

Thank you

    
asked by anonymous 18.06.2015 / 04:27

1 answer

2

You can achieve the desired using the element input type='checkbox'

JS code taken from here: link

function teste() {
  var choices = [];
  var els = document.getElementsByName('check');
  for (var i = 0; i < els.length; i++) {
    if (els[i].checked) {
      choices.push(els[i].value);
    }
  }
  alert(choices);
}
<input type="checkbox" value="Brasil" name="check">Brasil
<br/>
<input type="checkbox" value="Portugal" name="check">Portugal
<br/>
<input type="checkbox" value="Espanha" name="check">Espanha
<br/>
<input type="checkbox" value="França" name="check">França
<br/>
<input type="checkbox" value="Alemanha" name="check">Alemanha
<br/>
<input type="button" value="checar" onclick="teste()">

Explaining line by line what the code does:

WiththisrequirementofMINIMUM2SELECTEDFIELDSANDMAXIMUM2SELECTEDFIELDSyoucan:

  • Thereareotherways,perhapsevensimpler.

function teste() {
  var choices = [];
  var els = document.getElementsByName('check');
  for (var i = 0; i < els.length; i++) {
    if (els[i].checked) {
      choices.push(els[i].value);
    }
  }
  if (choices.length <= 1 || choices.length > 2) {
    alert('necessario 2 campos selecionados');
  }else{
    alert('ok, window.submit();');
  }
  alert(choices);
}
    <input type="checkbox" value="Brasil" name="check">Brasil
    <br/>
    <input type="checkbox" value="Portugal" name="check">Portugal
    <br/>
    <input type="checkbox" value="Espanha" name="check">Espanha
    <br/>
    <input type="checkbox" value="França" name="check">França
    <br/>
    <input type="checkbox" value="Alemanha" name="check">Alemanha
    <br/>
    <input type="button" value="checar" onclick="teste()">
    
18.06.2015 / 04:55