update combox after closing modal bootstrap

1

Colleagues,

I have a system where the customer clicks a button, opens a modal bootstrap for him to register a story. I need to make that when doing the register, the combox where the materials are updated.

I tried the following code:

<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 place the 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>
    
asked by anonymous 31.01.2017 / 13:39

1 answer

2

I think this is it, if I understood 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><scriptsrc="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>
    
31.01.2017 / 14:00