How to activate the modal through the option in a select?

Asked

Viewed 1,358 times

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>

1 answer

5


You need to put a value in options, and create the div with the modal. In the example below I used the modal of documentation adapted to your code (see below by clicking on "Execute code 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>
<script src="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>

  • Thank you so much for the help! The problem is that I want the modal to appear only after I have selected the option "Memory", understand? This example that you made the modal already appears when you click the selects (without having chosen an option). You could help me with this?

  • Thank you very much, Renan. It worked perfectly the way I wanted it :)

  • Thanks @Renan! I edited the answer. I was here trying to solve this, and had already tried both, but separately. :-)

  • I didn’t have to, don’t worry! Thanks! The important thing is to learn! But then I erase my tbm, so it doesn’t make any sense! Thanks! @Renan

  • That’s it! Thanks guys. See you around ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.