我已经阅读了几乎所有关于此的可用主题。他们中的大多数人重复“你应该写 .on('click' function(){}) 并解决你的问题。所以 - 我动态添加了按钮,我已经为这个按钮定义了一个事件。会发生什么me - 单击按钮 -> 将页面重定向到末尾带有问号的相同 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">
听起来您正在导致页面由于浏览器表单提交而重新加载
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句