Select a certain number of Inputs from an option

0

search the forum but I did not find a solution, what I'm looking for is that from an option that has the numbers 1, 3 and 5 defined, you can select the same quantities in a list with inputs to send a form by mail, I appreciate the help

<li>
<label>No. de Seminarios de tu interes <span class="required">*</span></label>
  <select name="seminarios" name="seminarios" id="seminarios" >
    <option>1</option>
    <option>3</option>
    <option>5</option>
  </select>
</li>

  • <li> Selecciona los seminarios
    <p><input type="checkbox" value="seminario1">Seminario 1</p>
    <p><input type="checkbox" value="seminario2">Seminario 2</p>                            
    <p><input type="checkbox" value="seminario3">Seminario 3</p>                            
    <p><input type="checkbox" value="seminario4">Seminario 4</p>                            
    <p><input type="checkbox" value="seminario5">Seminario 5</p> 
    </li>
    

    and as such the only code that arrived was this but I think it's not the right one

    $(document).ready(function(){ 
    $(‘#cantidad’).change(function(){
    var valor= $(“#cantidad”).val();
    if (valor==4){ $(‘#seminarios’).selectmenu( “1” ); 
    }else{ 
    $(‘#seminarios’).selectmenu( “3” ); }
    })
    })
    

    Thank you!

        
  • asked by Seruf 29.11.2018 в 21:56
    source

    1 answer

    0

    I made some modifications to both your code JS and your HTML , I added a value to your options and changed the one of your checkbox , this in order to be able to compare this value with each one of the options:

    $('#seminarios').change(function(){
      var valor= $('#seminarios').val();
      
      //console.log(valor);
      
       $('input:checkbox').each(function(e){
        if($(this).val() == valor){
            $(this).attr("checked", "checked");
        }
    });
      
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <li>
    <label>No. de Seminarios de tu interes <span class="required">*</span></label>
      <select name="seminarios" id="seminarios" >
        <option disabled selected>--Seleccione--</option>
        <option value="1">1</option>
        <option value="3">3</option>
        <option value="5">5</option>
      </select>
    </li>
    
    <li> Selecciona los seminarios
    <p><input type="checkbox" value="1">Seminario 1</p>
    <p><input type="checkbox" value="2">Seminario 2</p>               
    <p><input type="checkbox" value="3">Seminario 3</p>              
    <p><input type="checkbox" value="4">Seminario 4</p> 
    <p><input type="checkbox" value="5">Seminario 5</p> 
    </li>

    What I did was to obtain the value of option , go through each each of the checkbox and mark that option that matches this value obtained from option . I leave you the documentation of the function each : link

    I commented that you have syntax errors in your JQuery , since the quotes you use are not the ones indicated.

    I hope it helps you. Greetings.

        
    answered by 29.11.2018 в 23:49