gpt4 book ai didi

javascript - JQuery/Ajax 函数不适用于生成的新行

转载 作者:行者123 更新时间:2023-12-02 15:21:17 25 4
gpt4 key购买 nike

我正在尝试为动态表实现一个函数,该表如下所示:

<table>
<thead><tr><th>Código</th><th>Nombre</th><th>Cantidad</th><th>Precio</th></tr></thead>
<tbody id="bodyVender">
<tr>
<td><input id="inp0" type="text" autofocus="true" class="form-control"></td> <td id="tdN0"></td> <td><input id="tdC0" type="text" class="form-control"></td> <td id="tdP0"></td>
</tr>
</tbody>
</table>

然后,当调用 JQuery 函数时,我会向 tbody 添加更多表行,如下所示:

$('#bodyVender').append('<tr><td><input id="inp'+count+'" type="text" class="form-control"></td> <td id="tdN'+count+'"></td> <td><input id="tdC'+count+'" type="text" class="form-control"></td> <td id="tdP'+count+'"></td></tr>');

如您所见,创建的新行将包含 tds 和输入,其新 ID 由“count”变量确定,因此每次函数调用后输入 ID 可能如下所示: inp0 、 inp1 、 inp2 。

这可行,但只是第一次,即使我为新创建的 ID 调用该函数也是如此。

我使用 $(document).on 来调用该函数,我认为这应该适用于创建的新行。

这是完整的代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Panel de Administración</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="estilo3.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(Principal);

function Principal(){
var count = 0; //sell's counter

$(document).on('keydown','#inp'+String(count), function(e){ //when press key on input with id="inp0"
if(event.keyCode == 13){ //if the key pressed is Enter
VenderConsulta($(this).val(),count); //call function VenderConsulta with the typed code
}
});

function VenderConsulta(Codigo,ntd){
datos={codigo:Codigo}; // the code to send
$.ajax({
url:"bringMeTheData.php",
type: "POST",
data: datos,
success: function(datos){
console.log(datos); //i'm recibing something like [{"Nombre":"A product name","P_venta":"990"}]
count+=1; //next time i'll choose the new input with id="inp1"
$(':focus').blur(); //blur the input
var arr = JSON.parse(datos);
var tdNombre = arr[0].Nombre;
var tdPrecio = arr[0].P_venta;
$('#tdN'+ntd+'').html(tdNombre);
$('#tdC'+ntd+'').val(1);
$('#tdP'+ntd+'').html(tdPrecio);

$('#bodyVender').append('<tr><td><input id="inp'+count+'" type="text" class="form-control"></td> <td id="tdN'+count+'"></td> <td><input id="tdC'+count+'" type="text" class="form-control"></td> <td id="tdP'+count+'"></td></tr>');

$('#inp'+count).focus(); //setting focus to the new created input
}
});
}
}

</script>
</head>

<body>
<div class="container-fluid">
<section class="tablaVender">
<div class="row">
<div class="table-responsive" style="background:white">
<h3 style="margin-left:15px"> Venta de productos </h3>
<table class='table table-hover table-bordered'>
<thead><tr><th>Código</th><th>Nombre</th><th>Cantidad</th><th>Precio</th></tr></thead>
<tbody id="bodyVender">
<tr> <!-- This is the input and i'll add more trs like this in the function VenderConsulta -->
<td><input id="inp0" type="text" autofocus="true" class="form-control"></td> <td id="tdN0"></td> <td><input id="tdC0" type="text" class="form-control"></td> <td id="tdP0"></td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</div>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>

为什么这第一次有效,然后就不再有效了?我对创建的新行的操作有误吗?抱歉我的英语不好,谢谢。

最佳答案

您的 keydown 功能选择器只会在第一次输入时触发它。您只需调用 .on() 一次,并将其指定为 '#inp'+String(count) 作为选择器。此时您的变量 count 为 0,因此它仅适用于 ID 为 inp0 的输入。您可以使用适用于所有 input[x] id 的选择器来解决此问题。检查 id 开头的属性选择器可以工作。喜欢:

  $(document).on('keydown','[id^=inp]'function(e){  //when press key on input with id="inp0"
if(event.keyCode == 13){ //if the key pressed is Enter
VenderConsulta($(this).val(),count); //call function VenderConsulta with the typed code
}
});

关于javascript - JQuery/Ajax 函数不适用于生成的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34034119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com