使用 jQuery 以编程方式添加事件时,不会在输入上触发事件
您好 StackOverflow,
我想知道是否有办法在使用 jQuery 以编程方式添加控件(在本例中为输入文本框)后触发事件。我的意思是,我在表中使用 jQUery.after() 添加了一行填充 s 和输入的表,并且该新行中的控件不会触发任何事件。
我真的需要这方面的帮助,经过大量“谷歌搜索”后我无法弄清楚,我不能再为此挣扎了。一些帮助将非常感激。
如果还不够清楚,请不要介意提出任何问题以使其更清楚。
编辑(这是我添加行的操作)
$(".prodNum").on("change", function(){
//Envoie le sku du produit au serveur et affiche les infos lorsque le produit est valide
$.post("../PHP/caisse.php", {productSku : $(this).val()},
function(data){
var total = 0;
var jsonObj = $.parseJSON(data);
var newRow = "";
if( typeof jsonObj.message !== "undefined")
{
alert(jsonObj.message);
}
else
{
total = jsonObj.price;
$("#desc" + numberOfProducts ).val(jsonObj.description);
$("#prix" + numberOfProducts ).val(roundToDecimals(jsonObj.price,2));
$("#qte" + numberOfProducts ).val(1);
$("#total" + numberOfProducts ).val(roundToDecimals(total,2)).change();
newRow = addProductRow(numberOfProducts);
$("#prod" + numberOfProducts ).after(newRow);
numberOfProducts++;
}
});
});
这是 addProductRow 函数
function addProductRow(numOfProd)
{
if( numOfProd % 2 == 0 )
{
newRow = "<tr class=\"alternate1\" id=\"prod" + numOfProd + "\">";
}
else
{
newRow = "<tr class=\"alternate2\">";
}
newRow += "<td><input type=\"text\" class=\"prodNum\" name=\"prodNum[]\" id = \"prodNum" + (numOfProd + 1) + "\" /></td>";
newRow += "<td class=\"middle\"><input type=\"text\" class=\"desc\" id=\"desc" + (numOfProd + 1) + "\" /></td>";
newRow += "<td class=\"middle\"><input type=\"text\" class=\"price\" id=\"prix" + (numOfProd + 1) + "\" /></td>";
newRow += "<td class=\"middle\"><input type=\"text\" class=\"qty\" id=\"qte" + (numOfProd + 1) + "\" /></td>";
newRow += "<td class=\"middle\"><input type=\"text\" class=\"total\" name=\"total[]\" id=\"total" + (numOfProd + 1) + "\" /></td></tr>";
return newRow;
}
预期的结果是新行可以在其“prodNum”输入上使用“change”事件。
谢谢你,
PBaller
Hello StackOverflow,
I'd like to know if there is way to trigger an event after the control (in this case an input textbox) has been added programmatically using jQuery. What I mean is, I added a row filled with s and inputs to a table using jQUery.after() in a table and the controls in this new row won't trigger any event.
I really need help on this one, I couldn't figure it out after a lot of "googling" and I can't struggle on that anymore. Some help would be really appreciated.
If it isn't clear enough, don't mind to ask any question to make it clearer.
EDIT (here's what I do to add a row)
$(".prodNum").on("change", function(){
//Envoie le sku du produit au serveur et affiche les infos lorsque le produit est valide
$.post("../PHP/caisse.php", {productSku : $(this).val()},
function(data){
var total = 0;
var jsonObj = $.parseJSON(data);
var newRow = "";
if( typeof jsonObj.message !== "undefined")
{
alert(jsonObj.message);
}
else
{
total = jsonObj.price;
$("#desc" + numberOfProducts ).val(jsonObj.description);
$("#prix" + numberOfProducts ).val(roundToDecimals(jsonObj.price,2));
$("#qte" + numberOfProducts ).val(1);
$("#total" + numberOfProducts ).val(roundToDecimals(total,2)).change();
newRow = addProductRow(numberOfProducts);
$("#prod" + numberOfProducts ).after(newRow);
numberOfProducts++;
}
});
});
And here's addProductRow function
function addProductRow(numOfProd)
{
if( numOfProd % 2 == 0 )
{
newRow = "<tr class=\"alternate1\" id=\"prod" + numOfProd + "\">";
}
else
{
newRow = "<tr class=\"alternate2\">";
}
newRow += "<td><input type=\"text\" class=\"prodNum\" name=\"prodNum[]\" id = \"prodNum" + (numOfProd + 1) + "\" /></td>";
newRow += "<td class=\"middle\"><input type=\"text\" class=\"desc\" id=\"desc" + (numOfProd + 1) + "\" /></td>";
newRow += "<td class=\"middle\"><input type=\"text\" class=\"price\" id=\"prix" + (numOfProd + 1) + "\" /></td>";
newRow += "<td class=\"middle\"><input type=\"text\" class=\"qty\" id=\"qte" + (numOfProd + 1) + "\" /></td>";
newRow += "<td class=\"middle\"><input type=\"text\" class=\"total\" name=\"total[]\" id=\"total" + (numOfProd + 1) + "\" /></td></tr>";
return newRow;
}
The result expected is that the new row can use the "change" event on its "prodNum" input.
Thank you,
PBaller
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
live
而不是bind
-请注意,在 jQuery 1.7 中,.
live()
方法已被弃用。因此,如果您使用 jQuery 1.7 或更高版本,请使用
.on()
。http://api.jquery.com/live/
http://api.jquery.com/on/
Try
live
instead ofbind
-Note that of jQuery 1.7, the .
live()
method is deprecated.So if you are using jQuery 1.7 or later use
.on()
.http://api.jquery.com/live/
http://api.jquery.com/on/