ÏÖÔÚ¼òµ¥½éÉÜÒ»ÏÂʾÀýÓ¦ÓóÌÐò¡£ÎÒ½«ÔÚ±¾ÏµÁÐËùÓÐÎÄÕÂÖÐʹÓÃÕâ¸öʾÀýÓ¦ÓóÌÐò£¬ÒòΪËüʹÓÃÁË´óÁ¿²»Í¬µÄ jQuery ʾÀý£¬²¢ÇÒ¼¸ºõËùÓÐÈ˶¼ÊìϤÕâ¸öÓ¦ÓóÌÐò ¡ª Ò»¸ö´¦Àí Web ÓʼþµÄ¸» Internet Ó¦ÓóÌÐò¡£Õâ¸öʾÀýÓ¦ÓóÌÐòÊÇÒ»¸ö¼òµ¥µÄÓʼþ¿Í»§»ú£¬ËüÀûÓà jQuery ¸øÓû§ÕâÑùµÄ¸Ð¾õ£º¸Ãµç×ÓÓʼþ¿Í»§»ú·Ç³£ÀàËÆÓÚ×ÀÃæÓ¦ÓóÌÐò¡£ÔÚ×îºóһƪÎÄÕ½áÊøʱ£¬Äú½«Ã÷°×Õâ¸ö¼òµ¥µÄÓ¦ÓóÌÐòÊÇÈçºÎΪÓû§ÖÆÔìÕâÖָоõµÄ£¬²¢ÇÒÃ÷°×ʹÓà jQuery ʵÏÖÕâ¸ö¹¦ÄÜÊǶàô¼òµ¥¡£
±¾ÎĵÄÖصãÊÇ ¡°Select All¡±/¡°Deselect All¡± ¸´Ñ¡¿ò£¬ËüÃdzöÏÖÔÚ Web Óʼþ±í£¨ÏÂÃæÍ»³öÏÔʾ£©µÄ×ó²àÁеĶ¥²¿¡£µ±Ñ¡Öиø´Ñ¡¿òʱ£¬Ëü½«Ñ¡Ôñ¸ÃÁеÄÿ¸ö¸´Ñ¡¿ò£»È¡ÏûÑ¡Ôñ¸Ã¸´Ñ¡¿òʱ£¬Ëü½«È¡ÏûÑ¡Ôñ¸ÃÁеÄËùÓи´Ñ¡¿ò¡£
ͼ 2. ¡°Select All¡± ¸´Ñ¡¿ò
Çåµ¥ 13. ×ÛºÏѧµ½µÄ֪ʶ
<!-- The first step is creating the Select All checkbox itself. we give it a unique ID on the page --> <input type=checkbox id=selectall> <!-- The next step is giving each of the rows their own checkbox. we put each row's checkbox into the 'selectable' class, since there can be many rows, and we want each of the rows' checkboxes to have the same behavior. --> <input type=checkbox class=selectable> <!-- The final step is bringing it all together with some jQuery code. --> // remember that all jQuery setup code must be in this document.ready() function, // or contained within its own function in order to function correctly. $(document).ready(function(){ // We use the jQuery selection syntax to find the selectall checkbox on the page // (note the '#' which signifies ID), and we tell jQuery to call the selectAll() // function every time someone clicks on the checkbox (we'll get to Events in a // future article). $("#selectall").click(selectAll); }); // This function will get called every time someone clicks on the selectall checkbox function selectAll() { // this line determines if the selectall checkbox is checked or not. The attr() // function, discussed in a future article, simply returns an attribute on the // given object. In this case, it returns a boolean if true, or an undefined if // it's not checked. var checked = $("#selectall").attr("checked"); // Now we use the jQuery selection syntax to find all the checkboxes on the page // with the selectable class added to them (each row's checkbox). We get an array // of results back from this selection, and we can iterate through them using the // each() function, letting us work with each result one at a time. Inside the // each() function, we can use the $(this) variable to reference each individual // result. Thus, inside each loop, it finds the value of each checkbox and matches // it to the selectall checkbox. $(".selectable").each(function(){ var subChecked = $(this).attr("checked"); if (subChecked != checked) $(this).click(); }); }
¡¡