[英]Dynamically limit maximum number of checkboxes selectable
我發現許多腳本限制了可以在使用javascript的頁面中選擇的復選框的最大數量,例如,以下是其中之一:
http://www.plus2net.com/javascript_tutorial/checkbox-limit.php
但是我不需要一個特定的數字,而是一個可變的數字。
這意味着我需要有一個百分比限制,而不是一個數字。
例如,將用戶選擇限制為頁面中復選框的最大30%。
非常感謝您對如何實現這一目標的任何幫助!
要將腳本更改為相對於固定總數的百分比,您只需要占用元素總數的30%。
function chkcontrol(j) {
var length = document.form1.chb.length;
// Max number allowed checked is 30% of the total
var max = Math.round(length * .3);
var total = 0;
for(var i = 0; i < length; i++){
if(document.form1.ckb[i].checked){
total = total + 1;
}
if(total > max){
alert("Please Select only three")
document.form1.ckb[j].checked = false ;
return false;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.