想實(shí)現(xiàn)的最終效果:點(diǎn)擊某個(gè)復(fù)選框,取消其它選中狀態(tài),只選中當(dāng)前選框?qū)崿F(xiàn)代碼:<body> <input type="checkbox"> 復(fù)選框1 <input type="checkbox"> 復(fù)選框2 <input type="checkbox"> 復(fù)選框3 <input type="checkbox"> 復(fù)選框4 <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $('input:even').attr('checked', true); $('input').click(function(){ $('input').attr('checked', false); // $(this).attr('checked', true); this.checked = true; }) </script></body>對(duì)上述代碼提的幾個(gè)問題:1、點(diǎn)擊后重置所有選框?yàn)槲催x中$('input').attr('checked', false);而只選中當(dāng)前選框// 原生js實(shí)現(xiàn)this.checked = true;可問題就出在這里,直選中當(dāng)前選框的代碼為什么不能這樣寫:// jQuery實(shí)現(xiàn)$(this).attr('checked', true);2、如果設(shè)置為jQuery方式實(shí)現(xiàn)當(dāng)前選中,明明已經(jīng)設(shè)置了(看F12),可就是沒效果
不能理解的checkbox選中??
瀟瀟雨雨
2019-03-12 16:13:02