用checkbox复选框覆盖parrents onClick事件? [英] Override parrents onClick event with checkbox onClick?

查看:128
本文介绍了用checkbox复选框覆盖parrents onClick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的英语不好对不起。



我正在制作优惠券网站,并且无法选择和取消选择优惠券。每张优惠券都位于一个DIV盒子中,其中有一个复选框。

我在DIV框上做了一个onClick函数(这样用户就可以通过点击DIV框内的任何东西来选择优惠券,我现在需要的是,当用户想要取消选择优惠券(通过点击DIV框内的复选框),我需要'重写'DIV的onClick函数(执行复选框onClick事件,而不是DIV的onClick事件)。



我知道每个人都喜欢一些代码作为例子,但是问题很简单,我不认为你需要在事件/函数中使用所有不用的代码:)



谢谢:)

解决方案

好像你想要 stopPropagation 如果复选框未被选中: http://jsfiddle.net/8Dcq8/

  $(div)。click(function(){
alert(add); //点击div中的任何位置以添加优惠券
}); $(b)
$(:checkbox)。click(function(e){
if(!this.checked){//如果取消选中,取消优惠券
alert(remove );
e.stopPropagation(); //不运行父级onclick
}
});


First, sorry for my bad English.

I'm making a Coupons site and have trouble with selecting and deselecting the coupons. Each coupon is in a DIV 'box' in which there is a checkbox.

I made a onClick function on the DIV box (so the user can select the coupon by clicking on anything inside the DIV box. What I need now is, when the user want to deselect the coupon (by clicking on the checkbox inside the DIV box), I need to 'override' the DIV's onClick function (execute the checkbox onClick event, not the DIV's onClick event).

I know that everyone prefers some code as an example, but the question/problem is simple and I don't think you need all of my un'useless code inside the events/functions :)

Thanks :)

解决方案

It seems like you want stopPropagation if the checkbox is being unchecked: http://jsfiddle.net/8Dcq8/.

$("div").click(function() {
    alert("add"); // clicking anywhere in div to add coupon
});

$(":checkbox").click(function(e) {
    if(!this.checked) { // if unchecking, remove coupon
        alert("remove");
        e.stopPropagation(); // don't run parent onclick
    }
});

这篇关于用checkbox复选框覆盖parrents onClick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆