在網(wǎng)上找到的,可以使用。由于刷新后,多選框會保留選中的,這時數(shù)量達(dá)到要求后還是可以選擇的bug,在前面加個判斷數(shù)量是否數(shù)量達(dá)到要求的處理就可以了。
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標(biāo)題文檔</title><script src="http://code.jquery.com/jquery-1.4.2.min.js"></script><script>$(function(){//加上判斷是否達(dá)到數(shù)量要求 if($(":checkbox").size()>=3){ $(":checkbox").removeAttr("checked"); $(":checkbox").attr("disabled","disabled"); $(":checkbox").removeAttr("disabled"); } var num = 0; $(":checkbox").each(function(){ $(this).click(function(){ if($(this)[0].checked) { ++num; if(num == 3) { //alert("最多選擇 三項(xiàng) 的上限已滿, 其他選項(xiàng)將會變?yōu)椴豢蛇x."); $(":checkbox").each(function(){ if(!$(this)[0].checked) { $(this).attr("disabled", "disabled"); } }); } } else { --num; if(num <= 2) { $(":checkbox").each(function(){ if(!$(this)[0].checked) { $(this).removeAttr("disabled"); } }); } } }); });})</script> </head><body><input type="checkbox" />上網(wǎng)<br /><input type="checkbox" />旅游<br /><input type="checkbox" />逛街<br /><input type="checkbox" />游戲<br /><input type="checkbox" />聽歌<br /><input type="checkbox" />購物<br /></body></html>以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答