您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)jquery如何處理checkbox是否被選中,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
現(xiàn)在如果一個復(fù)選框被選中,是用checked=true,checked="checked"也行
要用prop代替attr會更好,雖然在jQuery1.6之前版本的attr()方法能正常使用,但是現(xiàn)在必須使用prop()方法代替
實(shí)例代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>checkbox</title> </head> <body> <input type="button" id="btn1" value="全選"> <input type="button" id="btn2" value="取消全選"> <input type="button" id="btn3" value="選中所有奇數(shù)"> <input type="button" id="btn4" value="反選"> <input type="button" id="btn5" value="獲得選中的所有值"> <input type="checkbox" value="checkbox1"/> <input type="checkbox" value="checkbox2"/> <input type="checkbox" value="checkbox3"/> <input type="checkbox" value="checkbox4"/> <input type="checkbox" value="checkbox5"/> <script src="js/jquery-3.2.0.min.js"></script> <script> $(function(){ var checkbox = $("input[type=checkbox]"); $("#btn1").on("click",function(){ checkbox.prop("checked",true); }); $("#btn2").on("click",function(){ checkbox.prop("checked",false); }); $("#btn3").on("click",function(){ $("input[type=checkbox]:even").prop("checked",true); }); $("#btn4").on("click",function(){ checkbox.each(function(){ if($(this).prop("checked")){ $(this).prop("checked",false); }else{ $(this).prop("checked",true); } }); }); $("#btn5").on("click",function(){ var str = ""; $("input[type=checkbox]").each(function(){ if($(this).prop("checked")){ str += $(this).val() + ","; } }); console.log(str); }); }); </script> </body> </html>
關(guān)于“jquery如何處理checkbox是否被選中”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。