最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - JQuery bind click event on div - Stack Overflow

matteradmin5PV0评论

I have some html like this:

          <div class="vmail vmail-bar normal-vmail-bar" id="pm_<?php echo $pm->to_id; ?>">
                <div class="checkbox"><input type="checkbox" class="checkbox" /></div>
                <div class="vmail-from">
                    <?php echo $pm->Prenume." ".$pm->Nume; ?>
                </div>
                <div class="vmail-content-title"><?php echo $pm->subject; ?> </div>

            </div>  

and the following Jquery:

 // Read message - bind click on entire div ( the div includes the checkbox)
  container.find('.vmail').bind('click', function() {
        var id = $(this).attr('id').replace("pm_","");
        getPM(id);
  });  


// bind click on checkbox
  container.find(':checkbox').bind('click', function() {
    if($(this).is(':checked')) {
     ...
    } else {
     ...
    }
  });  

both methods are tested and they worked, but after I did the bind on entire div (.vmail) , when I click on the input checkbox the event bined to the checkbox does not work and the event for the div is fired instead.

I want the event responsible for the checkbox click to fire and the div click to be canceled if I press click on the checkbox.

I have some html like this:

          <div class="vmail vmail-bar normal-vmail-bar" id="pm_<?php echo $pm->to_id; ?>">
                <div class="checkbox"><input type="checkbox" class="checkbox" /></div>
                <div class="vmail-from">
                    <?php echo $pm->Prenume." ".$pm->Nume; ?>
                </div>
                <div class="vmail-content-title"><?php echo $pm->subject; ?> </div>

            </div>  

and the following Jquery:

 // Read message - bind click on entire div ( the div includes the checkbox)
  container.find('.vmail').bind('click', function() {
        var id = $(this).attr('id').replace("pm_","");
        getPM(id);
  });  


// bind click on checkbox
  container.find(':checkbox').bind('click', function() {
    if($(this).is(':checked')) {
     ...
    } else {
     ...
    }
  });  

both methods are tested and they worked, but after I did the bind on entire div (.vmail) , when I click on the input checkbox the event bined to the checkbox does not work and the event for the div is fired instead.

I want the event responsible for the checkbox click to fire and the div click to be canceled if I press click on the checkbox.

Share Improve this question asked Dec 13, 2010 at 14:58 RevelationRevelation 891 gold badge3 silver badges11 bronze badges 1
  • it sounds stupid but give the css z-index a try! and put the check-box over the div! – user529649 Commented Dec 13, 2010 at 15:01
Add a ment  | 

1 Answer 1

Reset to default 4

You might want to look at the stopPropagation method on events. This will prevent it from 'bubbling' to parent elements.

container.find(':checkbox').bind('click', function(ev) {
    ev.stopPropagation();
    if($(this).is(':checked')) {
     ...
    } else {
     ...
    }
});  
Post a comment

comment list (0)

  1. No comments so far