Problem with click jquery

0

good I've been learning a bit of javascript and jquery for a week but I came to a problem that I could not find a solution for. What I try to do is to make a table from a table to edit its elements in this way: a table with several elements is shown, for each row there is a button, if the button is pressed that row is transformed into input values (to achieve writing in they), if the button is pressed again (a new one created with a different id), the data is sent to the database and then the row that was input in a row element of a table must be transformed again with the updated data. The problem is that the click function when clicking on the new button (with id button) does not do anything, not even show an alert or a log as indicated. Why is this happening? Thank you in advance

Function that receives the value of the fields when pressing the button for the second time (new button to confirm edition)

$(document).ready(function() {
    alert("jQuery está funcionando"); 
    jQuery("#boton").click(function(evento){
        evento.preventDefault();
        alert("se oprimio el boton"); 
    console.log("funciona"); 
        var especiee = jQuery("#especie").val();
        var generoo = jQuery("#genero").val();
        var familiaa = jQuery("#familia").val();
        var calidadd = jQuery("#calidad").val();
        var tamañoo = jQuery("#tamaño").val();
        var ciudadd = jQuery("#ciudad").val();
        var comentarioo = jQuery("#comentario").val();
        var precioo = jQuery("#precio").val();
});


                });

This function makes the form editable:

    function transformarEnEditable(nodo){
    //El nodo recibido es SPAN
    if (editando == false) {
    editando = "true";

    var nodoTd = nodo.parentNode; //Nodo TD
    var nodoTr = nodoTd.parentNode; //Nodo TR

    var nodosEnTr = nodoTr.getElementsByTagName('td');
    var especie = nodosEnTr[0].textContent; 
    var genero = nodosEnTr[1].textContent;
    var familia = nodosEnTr[2].textContent; 
    var calidad = nodosEnTr[3].textContent;
    var tamaño = nodosEnTr[4].textContent; 
    var ciudad = nodosEnTr[5].textContent;
    var comentario = nodosEnTr[6].textContent;
    var precio = nodosEnTr[7].textContent;
    var Editar = nodosEnTr[8].textContent;
    var nuevoCodigoHtml = '<td><input type="text" name="especie" id="especie" value="'+especie+'" size="17"></td>'+
    '<td><input type="text" name="genero" id="genero" value="'+genero+'" size="10"</td>'+
    '<td><input type="text" name="familia" id="familia" value="'+familia+'" size="10"</td>'+
    '<td><input type="text" name="calidad" id="calidad" value="'+calidad+'" size="5"</td> '+
    '<td><input type="text" name="tamaño" id="tamaño" value="'+tamaño+'" size="5"</td> '+
    '<td><input type="text" name="ciudad" id="ciudad" value="'+ciudad+'" size="10"</td> '+
    '<td><input type="text" name="comentario" id="comentario" value="'+comentario+'" size="20"</td> '+
    '<td><input type="text" name="precio" id="precio" value="'+precio+'" size="5"</td> '+
    '<td><input class="btn btn-primary"  id="boton" Value="aceptar" type="submit"></input></td>'+
    '<td><input class="btn btn-danger" value="eliminar" type="submit"></input> ' ;






nodoTr.innerHTML = nuevoCodigoHtml;}}

and with this I show the data:

<?php
    $conexion=mysqli_connect('localhost','root','','shells');

    $request=mysqli_query($conexion,"select especie, genero,familia,calidad,tamano,ciudad,comentario,precio from shell where habitad='land'");

    while($prueba=$request->fetch_assoc()){
?> 
    <tr class="info">

<td><?php echo $prueba['especie'] ;     ?>  </td> 
<td><?php echo $prueba['genero']   ;    ?>  </td> 
<td><?php echo $prueba['familia']   ;   ?>  </td> 
<td><?php echo $prueba['calidad']   ;   ?>  </td> 
<td><?php echo $prueba['tamano']   ;    ?>  </td> 
<td><?php echo $prueba['ciudad']   ;    ?>  </td> 
<td><?php echo $prueba['comentario'];   ?>  </td> 
<td><?php echo $prueba['precio']   ;    ?>  </td> 

<td><input type=submit name="editar" value="Editar" onclick="transformarEnEditable(this)" class="btn btn-primary"> </input> </td>
<td><input type="submit" name="eliminar" value="Eliminar" class="btn btn-danger"></input></td>
    </tr>

<?php   } ?>
    
asked by Javier Russell Richards Valenz 30.01.2017 в 16:22
source

3 answers

0

This is because when using the onReady button does not exist, then JQuery does not assign the click function, if you paste that piece of code

jQuery("#boton").click(function(evento){
    evento.preventDefault();
    alert("se oprimio el boton"); 
console.log("funciona"); 
    var especiee = jQuery("#especie").val();
    var generoo = jQuery("#genero").val();
    var familiaa = jQuery("#familia").val();
    var calidadd = jQuery("#calidad").val();
    var tamañoo = jQuery("#tamaño").val();
    var ciudadd = jQuery("#ciudad").val();
    var comentarioo = jQuery("#comentario").val();
    var precioo = jQuery("#precio").val();

});

Below your function you will see that it works

 function transformarEnEditable(nodo){
//El nodo recibido es SPAN
if (editando == false) {
editando = "true";

var nodoTd = nodo.parentNode; //Nodo TD
var nodoTr = nodoTd.parentNode; //Nodo TR

var nodosEnTr = nodoTr.getElementsByTagName('td');
var especie = nodosEnTr[0].textContent; 
var genero = nodosEnTr[1].textContent;
var familia = nodosEnTr[2].textContent; 
var calidad = nodosEnTr[3].textContent;
var tamaño = nodosEnTr[4].textContent; 
var ciudad = nodosEnTr[5].textContent;
var comentario = nodosEnTr[6].textContent;
var precio = nodosEnTr[7].textContent;
var Editar = nodosEnTr[8].textContent;
var nuevoCodigoHtml = '<td><input type="text" name="especie" id="especie" value="'+especie+'" size="17"></td>'+
'<td><input type="text" name="genero" id="genero" value="'+genero+'" size="10"</td>'+
'<td><input type="text" name="familia" id="familia" value="'+familia+'" size="10"</td>'+
'<td><input type="text" name="calidad" id="calidad" value="'+calidad+'" size="5"</td> '+
'<td><input type="text" name="tamaño" id="tamaño" value="'+tamaño+'" size="5"</td> '+
'<td><input type="text" name="ciudad" id="ciudad" value="'+ciudad+'" size="10"</td> '+
'<td><input type="text" name="comentario" id="comentario" value="'+comentario+'" size="20"</td> '+
'<td><input type="text" name="precio" id="precio" value="'+precio+'" size="5"</td> '+
'<td><input class="btn btn-primary"  id="boton" Value="aceptar" type="submit"></input></td>'+
'<td><input class="btn btn-danger" value="eliminar" type="submit"></input> ' ;
nodoTr.innerHTML = nuevoCodigoHtml;}

 jQuery("#boton").click(function(evento){
        evento.preventDefault();
        alert("se oprimio el boton"); 
    console.log("funciona"); 
        var especiee = jQuery("#especie").val();
        var generoo = jQuery("#genero").val();
        var familiaa = jQuery("#familia").val();
        var calidadd = jQuery("#calidad").val();
        var tamañoo = jQuery("#tamaño").val();
        var ciudadd = jQuery("#ciudad").val();
        var comentarioo = jQuery("#comentario").val();
        var precioo = jQuery("#precio").val();
});
}

You can also try changing the

jQuery("#boton").click(function(evento){

By

$( "boton" ).on( "click", {

From the onReady to see if this works without the need to embed it directly in the function

    
answered by 30.01.2017 в 16:46
0

The problem that I think is happening to you is that you are invoking an event that is tied to several elements (buttons) with the same id, in this case #boton, tries to invoke it by the class to see what happens, I tell you this is because when you bind an event for the id it is only tied to a single element (a single button) and for others so they have the same id it will not recognize, my recommendation is to invoke it for the class and not for the id since with the class if it can be tied to several elements. You tell us if he served you.

Original:

jQuery("#boton").click(function(evento){...

Suggestion:

jQuery(".boton").click(function(evento){...

    
answered by 05.09.2018 в 17:18
-1
<table id="table">
    <td><input class="btn btn-primary"  id="boton" Value="aceptar" 
type="submit"></input></td>
</table>
$("#table").on("click","#boton",function(){

})

The element to which you want to delegate the event is dynamic, the event must be delegated on an existing element such as the table, after indicating the event by placing a comma the dynamic element is placed, in this case the button. / p>     

answered by 02.06.2018 в 18:35