我有一个按钮可以删除其中包含的div。div是使用Jquery动态创建的,我正在使用它来删除每个div:
$('.shift_button').click(function(e) {
e.preventDefault();
$(this).parent().remove();
});
但是,当按下按钮时什么也没有发生,div仍然存在。知道我在做什么错吗?
以下是创建div的方法:
$('#shifts_to_complete').append('<div id="'+ length +'" class="current_shift" data-role="fieldcontain"></div>');
$('#'+ length +'').html('Location: ' + obj.shift_location + '<br>Date: ' + obj.shift_date + '<br>Shift Starts: ' + obj.start_time + '<br>Shift Ends: ' + obj.end_time + '<br>Hours Worked: ' + obj.duration + '<br>Pay for Shift: £' + shift_pay + '<br><button class="shift_button ui-btn ui-shadow ui-btn-corner-all ui-btn-up-b" id="btn_'+ length +'">Completed?</button>');
谢谢
使用事件委托:
$(document).on('click','.shift_button',function(e)
{
e.preventDefault();
$(this).parent().remove();
});
它将事件绑定到文档的所有将来动态元素,因为文档是始终存在的父元素。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句