如何防止表对同一条记录发出两次警报
How to prevent the table from alerting the same record twice?
我有一个在JavaScript中创建的表。单击按钮时,应该显示该行的行id,但单击按钮时,行id会收到两次警报。
下面是我的JavaScript代码:
newContent += Hesto.Html.CreateTD('<button type="button" value="Print" class="btnprint" onclick="test(this)">', null);
function test() {
$("button").click(function () {
var row = $(this).parents('tr').attr('id');
var rowtext = $(this).closest('tr').text();
alert(row);
});
}
它多次发出警报的原因是因为您多次委托同一事件,使用事件委托,并删除元素
上的onclick="test(this)"
$(document).on('click','.btnprint',function(){
var row = $(this).parents('tr').attr('id');
var rowtext = $(this).closest('tr').text();
alert(row);
});
在你的例子中,你可以调用按钮两次1)onclick="test(this)",2)button click
function test() {
var row = $(this).parents('tr').attr('id');
var rowtext = $(this).closest('tr').text();
alert(row);
}
否则你可以使用委托。@Anton张贴方法
$(document).on('click','.btnprint',function(){
var row = $(this).parents('tr').attr('id');
var rowtext = $(this).closest('tr').text();
alert(row);
});
function test() {
$(document).on('click', '.btnprint', function () {
var row = $(this).parents('tr').attr('id');
var rowtext = $(this).closest('tr').text();
alert(rowtext);
});
}
相关文章:
- 防止双击执行两次jQuery post请求
- 如何防止map.on(“click”)在触摸板上点击两次
- 防止数组项被选中两次
- 若在两个不同的选项卡中打开同一个页面,如何防止javascript运行两次
- 如何防止在快速单击时两次调用en事件处理程序
- 有没有一种不那么黑客的方法可以防止我的 on('click') 触发两次
- 防止 NaN 被先按或按两次
- 防止默认,直到连续两次单击
- 防止脚本在执行时运行两次
- 如何防止 iframe 运行两次加载事件
- 防止在引导选项卡中加载动态连续网络两次
- 防止源加载两次
- 如何防止jquery验证发出两次警报
- 如何防止访客用户对帖子进行两次投票
- 防止ajax调用触发两次
- 两个事件处理程序和一个函数.如何防止代码执行两次
- 防止在angularjs中单击按钮两次
- AngularJS.使用缓存时,防止加载JSON数据两次
- 使用Cookies防止用户点击链接两次
- 防止在两个触发事件中执行两次函数