How to activate modal through the option in a select?

7

Example: When I select the "volvo" option, the modal appears ...

<label>Recinto</label>
<select class="form-control" data-toggle="modal" data-target="#cria-recinto">
   <option data-toggle="modal" data-target="#cria-recinto">O meu coliseu</option>
   <option>volvo</option>
   <option>A minha arena</option>
   <option>a garagem da minha vizinha</option>
   <option>Na esquina</option>
</select>

<script type="text/javascript">
   $('select').change(function () {
      if ($(this).val() == "volvo") {
         $('#cria-recinto').modal('show');
      }
   });
</script>
    
asked by anonymous 13.01.2016 / 03:45

1 answer

5

You need to put value in options , and create div with modal . In the example below I used the modal documentation tailored to your code (see below by clicking on "Run snippet").

   $('select').on('change', function () {
   if ($(this).val() == "volvo") {
   $('#cria-recinto').modal('show');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<label>Recinto</label>
   <select class="form-control" data-toggle="modal">
      <option value ="coliseu">O meu coliseu</option>
      <option value="volvo">volvo</option>
      <option value="minhaarena">A minha arena</option>
      <option value="garagem">a garagem da minha vizinha</option>
      <option value="esquina">Na esquina</option>
   </select>

    
<!-- Modal -->
<div class="modal fade" id="cria-recinto" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
    
13.01.2016 / 06:10