Add fields with Jquery

Asked

Viewed 82 times

-1

I am using a Jquery command to dynamically add and remove fields in a form.

$(document).ready(function(){
    var _espc_clone_index=0;
    $("#add_espc").click(function(){
        _espc_clone_index++;
        $(this).parent().after($("#_espc").clone().attr("id","_espc" + 
        _espc_clone_index));
        $("#_espc" + _espc_clone_index).css("display","inline");
        $("#_espc" + _espc_clone_index + " input").attr("id", 
        "remover_espc" + _espc_clone_index);
        $("#remover_espc" + _espc_clone_index).click(function(){
            $(this).closest("div").remove();
        });
    });
    $("#btn_enviar").on("click", function() {
        alert($("#form_teste").serialize());
    });
});
.hidden {
    display: none;
}
<div id="_espc" class="hidden">
          <label for="espc">Especialidades</label>
          <select name="espc[]" id="espc">
          <?php
          include('conectadb.php');
          $pesquisa="select codigo_esp,tipo from especialidades order by 
          tipo";
          $query=mysqli_query($conn,$pesquisa);
          while($dados=mysqli_fetch_array($query))
          {
              $codigo=$dados['codigo_esp'];
              $tipo=$dados['tipo'];
              echo "<option value='" .$codigo ."'>" .$tipo ."</option>";
          }
          ?>
          </select>
        <input type="button" id="remover_espc" value="Remover">
</div>
<form>
<p>
          <label for="espc">Especialidades</label>
          <select name="espc[]" id="espc">
          <?php
          include('conectadb.php');
          $pesquisa="select codigo_esp,tipo from especialidades order by 
          tipo";
          $query=mysqli_query($conn,$pesquisa);
          while($dados=mysqli_fetch_array($query))
          {
              $codigo=$dados['codigo_esp'];
              $tipo=$dados['tipo'];
              echo "<option value='" .$codigo ."'>" .$tipo ."</option>";
          }
          ?>
          </select>
      <input type="button" id="remover_espc" value="Remover">
      <input type="button" value="Add Especialidade" id="add_espc">
</p>
<p>
    <input type="button" value="Enviar" id="btn_enviar" />
</p>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

So when I click to add, it creates a field at the bottom of the page after the form. I need him to create another field on the same line next to the original.

  • What html is being created ? What html is desired ? Exemplify both so that it is clear what you have now and what you want to happen.

  • I added html, and improved the explanation.

1 answer

0

The problem with your code is that the HTML structure was invalid. I noticed you put the tag <form>, then tagged <p> for each row of fields, but you were closing the tag <p> without opening it. What I did was include the opening of the tag <p> just after opening the tag <form> and your Javascript worked as expected.

Below is your code with that change:

$(document).ready(function(){
    var _espc_clone_index=0;
    $("#add_espc").click(function(){
        _espc_clone_index++;
        $(this).parent().after($("#_espc").clone().attr("id","_espc" + 
        _espc_clone_index));
        $("#_espc" + _espc_clone_index).css("display","inline");
        $("#_espc" + _espc_clone_index + " input").attr("id", 
        "remover_espc" + _espc_clone_index);
        $("#remover_espc" + _espc_clone_index).click(function(){
            $(this).closest("div").remove();
        });
    });
    $("#btn_enviar").on("click", function() {
        alert($("#form_teste").serialize());
    });
});
.hidden {
    display: none;
}
<div id="_espc" class="hidden">
    <p>
          <label for="espc">Especialidades</label>
          <select name="espc[]" id="espc">
          </select>
        <input type="button" id="remover_espc" value="Remover">
</div>
<form>
<p>
          <label for="espc">Especialidades</label>
          <select name="espc[]" id="espc">

          </select>
      <input type="button" id="remover_espc" value="Remover">
      <input type="button" value="Add Especialidade" id="add_espc">
</p>
<p>
    <input type="button" value="Enviar" id="btn_enviar" />
</p>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  • I did this way, when clicking to add creates a field in the line below and the next ones are created next to the first one that was created the way I wanted, which I need to change so that the first one is next to the 'add specialty' button and the next ones in the same direction?

Browser other questions tagged

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