English 中文(简体)
选择所有检查箱的必要性,并动态地为每个检查箱增加特定活动手
原标题:Need to select all checkboxes and dynamically add specific event handler to each
  • 时间:2011-06-27 13:12:47
  •  标签:
  • jquery

Given the following repeated markup in a page:

<div>
     <input type="checkbox" id="chk1" />
     <div id="div1" >
          //stuff to toggle
     </div>
</div>
<div>
     <input type="checkbox" id="chk2" />
     <div id="div2" >
          //stuff to toggle
     </div>
</div>

如何获得所有检查箱,然后在每次检查箱的笔记中,积极增加有关 d的角子?

$(document).ready(function() {
     $( input:checkbox ).each(
          function() {
               $(this).bind( click", function() {
                    //Attach toggle() to associate DIV
                    //$( #div1 ).toggle();
               });
          });
});

如果不采用按规定分类或假设已订购的身份证清单,这样做是否简单?

问题回答
  • Use the change event, since that s really what you care about
  • Use .live() so you only bind 1 listener, rather than 1 per checkbox
  • 利用DOM 探测器查找<div>

    $(function ()
    {
        $( input:checkbox ).live( change , function ()
        {
            $(this).next().toggle();
        });
    });
    

不需要<代码>each(或bind():

$( input:checkbox ).click(function (event) {
  //... using $(this)
});




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签