update Combox after closing the modal bootstrap

Asked

Viewed 647 times

1

Colleagues,

I have a system where the customer clicking a button opens a modal bootstrap for him to register a matter. I need to make sure that when registering, the Box where the materials are.

I tried the code below:

<div id="myModal" class="modal fade" role="dialog">
     <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header" style="background-color: #CF850F; color: #FFF; font-weight: bold">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Cadastrar nova matéria</h4>
      </div>
      <div class="modal-body">   
          <div class="form-group">
              <form method="post" name="post" id="contact-form">  
                <div id="success"></div>
                <label for="exampleInputEmail1">Matéria:</label>
                <div class="input-group">
                  <div class="input-group-addon">
                    <i class="fa fa-arrow-circle-right" aria-hidden="true"></i>
                  </div>
                  <input type="text" class="form-control" name="NovaMateria" maxlength="150" id="novovalor">
                </div>
              </form>   
              </div>
      </div>
      <div class="modal-footer">
       <button type="submit" class="btn btn-warning" data-dismiss="modal" id="submit"><i class="fa fa-floppy-o fa-lg" aria-hidden="true"></i> Salvar</button> <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
      </div>
    </div>
  </div>
</div>

When I put text field and put commented code, it works:

<script>
// Atualizar
   /* $(function(){
    $("#submit").click(function(){
        $("#materia").val($("#novovalor").val());

    });
});
*/
$(document).ready(function(){
    $("#submit").click(function(){  
        $('#novovalor').change(function(){
            $('#materia').load($('#novovalor').val());
        });
    });
 });  
</script>

And the Combox:

<select name="Materias" class="form-control select2" id="materia" data-placeholder="Selecione a matéria" style="width: 100%;">
............
</select>
  • If I understand correctly, you can already take the return of the modal and need to add in combobox.. that being so, $("#materia").append("<option value='" + id_novovalor + "'>" + novovalorretornadodomodal + "</option>");

1 answer

2


I guess this is it, if I understand correctly.

$("#myModal").on("hidden.bs.modal", function () {
    var new_val = $('#novovalor').val();
    $('#materia').append('<option selected>' +new_val+ '</option>');
    $('#opt-remove').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div id="myModal" class="modal fade" role="dialog">
     <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header" style="background-color: #CF850F; color: #FFF; font-weight: bold">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Cadastrar nova matéria</h4>
      </div>
      <div class="modal-body">   
          <div class="form-group">
              <form method="post" name="post" id="contact-form">  
                <div id="success"></div>
                <label for="exampleInputEmail1">Matéria:</label>
                <div class="input-group">
                  <div class="input-group-addon">
                    <i class="fa fa-arrow-circle-right" aria-hidden="true"></i>
                  </div>
                  <input type="text" class="form-control" name="NovaMateria" maxlength="150" id="novovalor">
                </div>
              </form>   
              </div>
      </div>
      <div class="modal-footer">
       <button type="submit" class="btn btn-warning" data-dismiss="modal" id="submit"><i class="fa fa-floppy-o fa-lg" aria-hidden="true"></i> Salvar</button> <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
      </div>
    </div>
  </div>
</div>

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>


<select name="Materias" class="form-control select2" id="materia" data-placeholder="Selecione a matéria" style="width: 100%;">
  <option id="opt-remove"></option>
  <option>Opt1</option>
  <option>Opt2</option>
</select>

  • Perfect Miguel. It worked!! Thanks again!

  • 1

    Not at all @Fox.11 , I wasn’t sure if this was it but I figured so. Good thing

Browser other questions tagged

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