Modal + form + values (bootstrap and javascript)

4

From the link below

link

I created the following modal

link

In the modal I fill in a value and it will update this value in the page form (which will have more fields ...)

Question : How do I do this using just Javascript, JQuery and Ajax?

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Modal</title>

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

</head>
</html>
<body>

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Atualizar valores
</button>

<div class="container">
  <form class="form-horizontal col-sm-2" action="." method="POST">
      <div class="form-group">
        <label for="id_price">Preço</label>
        <input type="text" id="id_price" name="movie" class="form-control">
      </div>

    <div class="form-group">
        <div class="">
          <button type="submit" id="id_submit" class="btn btn-primary">Salvar</button>
        </div>
      </div>
  </form>
</div>


<!-- Modal -->
<div class="modal fade" id="myModal" 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">Atualizar valores</h4>
      </div>
      <div class="modal-body">
        <input type="text" placeholder="Digite o valor">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
        <button type="button" class="btn btn-primary">Atualizar</button>
      </div>
    </div>
  </div>
</div>

</body>
    
asked by anonymous 05.10.2015 / 07:40

1 answer

3

Take a look at this example: link

No html:

 <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
        Atualizar valores
    </button>

    <div class="container">
        <form class="form-horizontal col-sm-2" action="." method="POST">
            <div class="form-group">
                <label for="id_price">Preço</label>
                <input type="text" id="id_price" name="movie" class="form-control">
            </div>

            <div class="form-group">
                <div class="">
                    <button type="submit" id="id_submit" class="btn btn-primary">Salvar</button>
                </div>
            </div>
        </form>
    </div>


    <!-- Modal -->
    <div class="modal fade" id="myModal" 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">Atualizar valores</h4>
                </div>
                <div class="modal-body">
                    <input id="novoValor" type="text" placeholder="Digite o valor">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                    <button id="btAtualizarValor" type="button" class="btn btn-primary" data-dismiss="modal">Atualizar</button>
                </div>
            </div>
        </div>
    </div>

No js:

$(function(){
    $("#btAtualizarValor").click(function(){
        $("#id_price").val($("#novoValor").val());
    });
});
    
05.10.2015 / 16:09