我正在学习jQuery,但遇到了问题。
这是代码
$(function(){
var gotProducts=new Array();
var productsWithDelete=new Array();
$('.addProducts').on('keyup',function(event) {
var searchVal=$(this).val().trim();
if(searchVal.length > 0) {
$.ajax({
url: 'http://localhost/url',
data: { products: $(this).val(), },
type: 'POST',
dataType: 'html',
success: function(msg) {
$('#printTheProducts').html(msg);
}
});
}
});
$('.productsButton').click(function() {
alert('yes');
});
});
我从ajax调用中得到的响应是一个具有productsButton类的按钮。现在,当我尝试单击该按钮时,我通过了ajax,那么它不会警告yes。我的意思是什么也没做。问题:-可能是什么问题?
尝试使用事件委托来.on()
生成按钮,因为它们是动态生成的
$('#printTheProducts').on('click','.productsButton',function(){
alert('yes');
});
#printTheProducts
最接近的父元素在哪里,您可以使用document
或document.body
用作选择器!
句法:
$(closestparentelement).on('event','targetselector',function(){
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句