SCRIPT LANGUAGE="JavaScript"!--//checkbox元素的名字前缀,本例为sample1,sample2,sample3...var sCtrlPrefix = "sample";//checkbox元素数量,本例有10个;var iMaxCheckbox = 10;//设置最大允许选择的数量;var iMaxSelected = 3;function doCheck(ctrl) {var iNumChecked = 0;var thisCtrl;var i;//初始化i = 1;//循环直到选中了最多的checkbox;while ((i = iMaxCheckbox) && (iNumChecked = iMaxSelected)) {thisCtrl = eval("ctrl.form." + sCtrlPrefix + i);if ((thisCtrl != ctrl) && (thisCtrl.checked)) {iNumChecked++;}i++;}// 检查是否达到了最大选择数量;if (iNumChecked == iMaxSelected) {// 如果是则uncheck刚选择的元素;ctrl.checked = false;ALERTXT="最多可选"+iMaxSelected+"个"alert(ALERTXT);}}// --/SCRIPTform name="form" method="post"input type="CHECKBOX" name="sample1" onClick="doCheck(this)"Ainput type="CHECKBOX" name="sample2" onClick="doCheck(this)"Binput type="CHECKBOX" name="sample3" onClick="doCheck(this)"Cinput type="CHECKBOX" name="sample4" onClick="doCheck(this)"Dinput type="CHECKBOX" name="sample5" onClick="doCheck(this)"Einput type="CHECKBOX" name="sample6" onClick="doCheck(this)"Finput type="CHECKBOX" name="sample7" onClick="doCheck(this)"Ginput type="CHECKBOX" name="sample8" onClick="doCheck(this)"Hinput type="CHECKBOX" name="sample9" onClick="doCheck(this)"Iinput type="CHECKBOX" name="sample10" onClick="doCheck(this)"J/form [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
SCRIPT LANGUAGE="JavaScript"var c=0,limit=3;function doCheck(obj) {obj.checked?c++:c--;if(climit)obj.checked=false,c--;}/SCRIPTform name="form" method="post"input type="CHECKBOX" name="sample1" onClick="doCheck(this)"Ainput type="CHECKBOX" name="sample2" onClick="doCheck(this)"Binput type="CHECKBOX" name="sample3" onClick="doCheck(this)"Cinput type="CHECKBOX" name="sample4" onClick="doCheck(this)"Dinput type="CHECKBOX" name="sample5" onClick="doCheck(this)"Einput type="CHECKBOX" name="sample6" onClick="doCheck(this)"Finput type="CHECKBOX" name="sample7" onClick="doCheck(this)"Ginput type="CHECKBOX" name="sample8" onClick="doCheck(this)"Hinput type="CHECKBOX" name="sample9" onClick="doCheck(this)"Iinput type="CHECKBOX" name="sample10" onClick="doCheck(this)"J/form [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
SCRIPT LANGUAGE="JavaScript"var c=0,limit=3;ALERTXT="最多可选"+limit+"个"function doCheck(obj) {obj.checked?c++:c--;if(climit)obj.checked=false,c--,alert(ALERTXT);}/SCRIPTform name="form" method="post"input type="CHECKBOX" name="sample1" onClick="doCheck(this)"Ainput type="CHECKBOX" name="sample2" onClick="doCheck(this)"Binput type="CHECKBOX" name="sample3" onClick="doCheck(this)"Cinput type="CHECKBOX" name="sample4" onClick="doCheck(this)"Dinput type="CHECKBOX" name="sample5" onClick="doCheck(this)"Einput type="CHECKBOX" name="sample6" onClick="doCheck(this)"Finput type="CHECKBOX" name="sample7" onClick="doCheck(this)"Ginput type="CHECKBOX" name="sample8" onClick="doCheck(this)"Hinput type="CHECKBOX" name="sample9" onClick="doCheck(this)"Iinput type="CHECKBOX" name="sample10" onClick="doCheck(this)"J/form [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]