標籤:style blog http color java io for ar 2014
代碼:
<h2>Default</h2>@using (Html.BeginForm()) { <ul> <li>@Html.CheckBox("subject") 語文</li> <li>@Html.CheckBox("subject")數學</li> <li>@Html.CheckBox("subject")英語</li> <li><a id="submit" href="http://www.cnblogs.com/wiming/">回到原點</a></li> </ul>}<script src="http://cdn.bootcss.com/jquery/2.1.0/jquery.min.js"></script><script type="text/javascript">//第一種方法:
$("#submit").click(function () { var objs = document.getElementsByName(‘subject‘); var isSel = false;//判斷是否有選中項,預設為無 for (var i = 0; i < objs.length; i++) { if (objs[i].checked == true) { isSel = true; break; } } if (isSel == false) { alert("請至少選擇一門科目"); return false; } else { return true; } })
//第二種方法:
$("#submit").click(function () {
if ($("input[type=‘checkbox‘]").is(‘:checked‘) == true) { return true; }
else {
alert("請至少選擇一門科目");
return false;
}
})</script>
效果:
js判斷checkbox是否已選