[英]Add event to Added dynamic DOM button - jQuery
我已经阅读了几乎所有可用的主题。 他们中的大多数人都重复使用“您应该编写.on('click'function(){}),它可以解决您的问题。因此-我动态添加了按钮,并且为该按钮定义了一个事件。我-单击按钮->在重定向之前,将页面重定向到末尾带有问号的相同URL-> localhost:3000 / blabla / bla ?,它会更改值,然后删除按钮的DOM容器,重定向我尝试了各种定义位置-首先在文档准备功能中,
然后在静态包装器中准备好
然后在准备好文档的静态包装器中
等等...
另外,我试图做一个外部功能。
<script>
$(function(){
$('#new-order').on('click',function(e) {
$('#bottom').slideToggle(1000);
});
// btn-delete-item event
// btn-increase event
$('#items').on('click','.btn-increase',function(){
console.log("HEY")
quantityLabel = $(this).parent().find('#line-item-quantity');
addedQntty = parseInt(quantityLabel.text())+1;
quantityLabel.text(addedQntty);
console.log('added quantity')
});
// btn-decrease event
// btn-add-item event
$('.btn-add-item').on('click',function(){
var prodID,quantity,html,itemID,itemTitle,itemPrice,itemDOM,prodPrice;
prodID = $(this).parent().attr('id');
itemTitle= $(this).parent().find('#title').text();
quantity = $(this).parent().find('input').val();
prodPrice= $(this).parent().find('#price').text();
itemPrice = parseInt(quantity)*parseInt(prodPrice);
itemDOM = $('#items').children();
itemDOM.length>0 ? itemID=itemDOM.last().attr('id').slice(-1) : itemID = 0;
html = `<div id="line-item-${itemID}">Title:<label id="line-item-name">${itemTitle}</label>Price:<label id="line-item-price">${itemPrice}</label>quantity<label id="line-item-quantity">${quantity}</label><button class="btn-decrease">-</button><button class="btn-increase">+</button><button class="btn-delete-item">delete</button></div>`;
$('#items').append($(html));
});
});
</script>
希望我尽可能明确地提出这个问题。
<button>
默认类型为“提交”
因此,要使用表单中的按钮进行除提交使用之外的其他操作
<button type="button">
听起来您由于浏览器表单提交而导致页面重新加载
在添加项目按钮的点击功能上,您尝试附加$(html)
,只需修复此问题然后再试一次,像这样写
$('#items').append(html);
您声明了一个变量“ html”,并在代码中尝试附加整个html。 希望修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.