Modal + form + values (bootstrap and javascript)

Asked

Viewed 8,275 times

4

From the following link

http://getbootstrap.com/javascript/#live-demo

I created the following modal

https://github.com/rg3915/front_end/blob/master/javascript/modal_form.html

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

Question: how do I do this using only 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>
  <script src="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>
  • I don’t understand very well what it will update. O input price? A database? A variable for calculations? What exactly?

1 answer

3


Take a look at this example: http://jsfiddle.net/emirdeliz/bbqmm3dt/

In 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());
    });
});

Browser other questions tagged

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