[英]Listview Checkbox for Selecting Rows is Raised 2 Times
$('#NewClientsTable').on('click', 'tr', function (e) {
var $r= $(this).closest('tr');
if ($r.find('input[type=checkbox]').eq(0).is(':checked'){
// Do Stuff
} else { // Do Stuff }
});
在此功能中,我会在ListView中捕获TR的任何点击:
在控制台中,我尝试通过Console.log()跟踪事件。但是我发现:
('#NewClientsTable')=> ItemTemplate中的表
只有单击复选框,我们才能一次触发该事件?
您需要在复选框事件处理程序中附加event.stopPropagation()
才能阻止冒泡。 假设您的复选框点击事件具有以下事件:
// selector and context depend on your original code
$('#NewClientsTable').on('click', 'input[type="checkbox"]', (function(e){
e.stopPropagation();
// another rest of the code
});
要阻止事件触发TR
标签子项,请执行以下操作:
$("#NewClientsTable tr input[type='checkbox']").click(function(event){
event.stopPropagation();
});
这是一个演示:
$("tr").click(function(event){ alert("works"); $(this).toggleClass("mycolor"); }); $("tr input[type='checkbox']").click(function(event){ event.stopPropagation(); $(this).closest('tr').toggleClass("mycolor"); });
.mycolor{ background-color:green;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr><td><input type="checkbox" name="check" ></td><td>test2</td></tr> <tr><td><input type="checkbox" name="check2" ></td><td>test2</td></tr> <table>
在此演示中,您会注意到,如果单击复选框,则什么都不会发生,但是,如果单击行,则会触发该事件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.