简体   繁体   English

Javascript单选按钮限制数量

[英]Javascript limit number of radio button picks

I have a an "entry form" with usually 32 choices, of which only 1 per can be chosen, so in total, 16 can actually be chosen. 我有一个“输入表”,通常有32个选择,每个只能选择1个,因此实际上总共可以选择16个。 (these numbers can vary slightly, week by week... think football games) What I want to do is limit this so that only a maximum of 8 can be chosen. (这些数字可能会略有不同,每周一次……想想看足球比赛)我想要做的是限制此限制,以便最多只能选择8个。

I have a piece of javascript that checks to see if all have been chosen, and tells the user if they have not... but I was trying to modify it a bit so that it will only allow the user select 8 choices. 我有一段JavaScript,可以检查是否所有内容都被选中,并告诉用户是否还没有选择……但是我试图对其进行一些修改,以便仅允许用户选择8个选择。

Here is the javascript: 这是JavaScript:

<script type="text/javascript">
            function checkform() {
                //make sure all picks have a checked value
                var f = document.entryForm;
                var allChecked = true;
                var allR = document.getElementsByTagName('input');
                for (var i=0; i < allR.length; i++) {
                    if(allR[i].type == 'radio') {
                        if (!radioIsChecked(allR[i].name)) {
                            allChecked = false;
                        }
                    }      
                }
                if (!allChecked) {
                    return confirm('Not all picks entered... Submit anyway?');
                }
                return true;
            }
            function radioIsChecked(elmName) {
                var elements = document.getElementsByName(elmName);
                for (var i = 0; i < elements.length; i++) {
                    if (elements[i].checked) {
                        return true;
                    }
                }
                return false;
            }
            </script>

Is there a way to do this or am I looking at it the wrong way? 有没有办法做到这一点,还是我看错了方向?

This should work and allow you to pass in the maximum number of picks allowed: 这应该可行,并允许您传递允许的最大选择次数:

<script>
    function checkform(maxPicks) {
  maxPicks = maxPicks || 8; //default

  //make sure all picks have a checked value
  var checkedCount = 0;
  var allR = document.getElementsByTagName('input');

  for(var i = 0; i < allR.length; i++) {
    var rad = allR[i];

    if (rad.checked) checkedCount++;
  }

  if (checkedCount < maxPicks) {
    return confirm('Not all picks entered... Submit anyway?');
  } else if (checkedCount > maxPicks) {
    alert('You may only choose up to ' + maxPicks + ' picks.');
    return false;
  }
  return true;
}
</script>

I would use checkboxes instead of radio buttons. 我会使用复选框而不是单选按钮。 Radio buttons imply that only one can be selected while checkboxes imply that multiples can be selected. 单选按钮表示只能选择一个,而复选框则表示可以选择多个。

Then the simplest thing to do is check how many are selected whenever the value of one changes. 然后,最简单的方法是检查一个值更改时选择了多少个。 If the number that are selected is equal to your maximum, disable the rest. 如果选择的数字等于您的最大数目,请禁用其余数目。 This allows the user to still deselect any that are already selected, but they will not be able to add any new selections. 这允许用户仍然取消选择任何已选择的内容,但是他们将无法添加任何新的选择。

You'll also want to re-enable when the number of selected are less than the max. 当所选的数量少于最大值时,您还需要重新启用。

var inputs = document.getElementsByTagName("input");

for(var i = 0; i < inputs.length; i++) {
    inputs[i].onclick = checkFunc;
}

function checkFunc() {
    count = 0;
    for(var i = 0; i < inputs.length; i++) {
        if(inputs[i].checked === true){
            count++;
        }
    }
    if (count > 3) {
        for(i = 0; i < inputs.length; i++) {
            if(inputs[i].checked === false){
                inputs[i].disabled = true;
            }
        }
    } else {
        for(i = 0; i < inputs.length; i++) {
            inputs[i].disabled = false;
        }
    }

}

Working JSFiddle . 工作中的JSFiddle This is not necessarily the most efficient implementation but shows how it would work. 这不一定是最有效的实现,但是显示了它如何工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM