元素未检测到单击事件
Element not detecting click event
我通过javascript动态生成HTML,并在一个表单元格中包含两个字形。铅笔字形响应正确,但我的删除代码没有,我不知道为什么。
这是呈现的HTML
<span id="edit-dp" class="glyphicon glyphicon-pencil" data-action-url="/Settings/GetDatapoint" data-id="3"></span>
<span id="delete-dp" class="glyphicon glyphicon-trash" data-id="3"></span>
下面是我的javascript代码,用于将事件绑定到元素。
$(document).ready(function(){
$('#delete-dp').click(function (event) {
alert('pressed');
//add the datapoint ID to the array to be serialised to JSON and then stored in a html hidden field
deletedDatapoints.push($(this).data('id'));
});
})
使用.on()
。
$(document).ready(function(){
$(document).on('click', '#delete-dp', function (event) {
alert('pressed');
//add the datapoint ID to the array to be serialised to JSON and then stored in a html hidden field
deletedDatapoints.push($(this).data('id'));
});
});
您可以将其范围扩大到非动态生成的最接近的父级,以便比document
更高效。
要处理动态添加的事件,请使用
$('#delete-dp').on('click',function(event){
//do your stuff
});
相关文章:
- 将单击事件添加到数据表
- 将单击事件添加到附加的项目中
- 在IE8中不起作用的元素上触发单击事件
- 链接上的IE10 jquery句柄单击事件不起作用
- 如何使用纯 JavaScript 在所有浏览器中区分左/右/中键单击事件
- 如何在master中调用细节网格作为单击事件
- 兰吉 |如何在 span 标签上使用单击事件删除突出显示的文本
- 如何隐藏按钮单击事件上的占位符
- 复选框,然后单击事件处理
- 如何使用for循环添加所有按钮'单击事件
- 手柄'img'单击事件并插入'alt'使用jQuery将属性转换为文本框
- 覆盖单击事件不起作用
- 单击事件打开两个弹出菜单
- 单击事件不起作用
- 从其他元素上的单击事件访问image src属性
- 单击事件中的ajax请求后重定向
- 如何缩放地图框传单中的标记单击事件
- 在没有焦点的情况下捕获画布上的第一次单击事件
- 通过单击事件识别画布上的对象
- 如何将单击事件添加到JQuery中动态添加的元素中