【发布时间】:2016-12-03 07:12:14
【问题描述】:
我试图在通过 JsRender 呈现后将点击监听器附加到页面上的 DOM 元素,但似乎遇到了一些困难..
标记:
<button id="placeBid" data-attr-saleId="{{:id}}" class="btn btn-lg btn-add-to-cart"><span class="glyphicon glyphicon-shopping-cart"></span>Bid on this item</button>
点击监听代码的Jquery:
$(document).ready(function(){
$('#placeBid').on('click','#templateContainer', function () {
console.log('Clicked place bid')
const saleId = $(this).attr('data-attr-saleId')
if (!saleId) return
$.ajax({
url: '/sale/placeBid',
data: {
bidAmount: $('#amount').val(),
hotelSale: saleId,
currency: $('#currencies:selected').val()
},
method: 'POST',
success: function (res, ts, xhr) {
if (xhr.status == 200 && res.status == 200) {
$.toaster({priority: 'info',message: 'Succesfully bidded on sale'})
}else {
//handle error
}
}
})
})
});
html 通过 jsRender 渲染到 templateContainer 模板中
const html = template.render({viewLocalsHere});
$('#templateContainer').html(html)
在 document.ready 函数中记录 console.log($('#placeBid')) 表明在附加 onclick 处理程序时正在检测到该元素,但是单击按钮不会记录 Clicked place bid
可能是什么问题?谢谢
【问题讨论】:
标签: jquery events onclick listener jsrender