简体   繁体   English

复选框:一次只允许一个复选框

[英]Check boxes: Allow for only one check box to be click at a time

I am currently working with check boxes and displaying values based on the values chosen. 我目前正在使用复选框并根据所选值显示值。 Almost everything is working in exception that i have to check boxes for method of shipping . 几乎所有的一切都在工作,除了我必须复选框method of shipping Right now the user can select two ways and this is an issue. 现在,用户可以选择两种方式,这是一个问题。 Is there a way to only allow one check box to be checked for method of shipping ? 有没有办法只允许检查一个复选框method of shipping EXAMPLE

JS JS

<script>
function check_value(currentElement, val, id, type) {
    var el = document.getElementById("imgBox" + id);
    if (val > 0 && val < 4) { //will trigger when [1,2,3]

        if(currentElement.checked)
        {
            el.src = "images/" + type + val + ".jpg";
            el.style.display = "";
        }
        else
        {
            el.src = "";
            el.style.display = "none";
        }

    }
}    
</script>

HTML HTML

<h2>Choose method of shipping</h2>
<form name="shipping_list">
    <input type="checkbox" name="shipping1" onclick='check_value(this, 1, "4", "shipping")'/> USPS Ground<br />
    <input type="checkbox" name="shipping2" onclick='check_value(this, 2, "4", "shipping")'/> FedEx         
</form>

<img id="imgBox4" src="#" style="display:none">

You should use radio buttons instead: 您应该使用单选按钮:

<form name="shipping_list">
    <input type="radio" name="shipping" id="shipping1" onclick='check_value(this, 1, "4", "shipping")'/> USPS Ground<br />
    <input type="radio" name="shipping" id="shipping2" onclick='check_value(this, 2, "4", "shipping")'/> FedEx         
</form>

The key is the name attribute, the value should be same for all the radio buttons that belong to one group. 键是name属性,对于属于一个组的所有单选按钮,该值应该相同。

单选按钮怎么样?

<input type="radio" name="shipping" onclick="check_value(this, 1, '4', 'shipping');">
$(function() { 
  $('input[type="checkbox"]').bind('click',function() {
    $('input[type="checkbox"]').not(this).prop("checked", false);
  });
});

暂无
暂无

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

相关问题 在一组复选框中,一次只能选择一个 - in a group of check boxes only allow one to be selected at a time 我正在尝试用随机数检查 25 个框,并且只有在另一个框已被单击时才能单击每个框? - I am trying to check 25 boxes with random numbers and only be able to click each box if another one was already clicked? 角度材料两个复选框,一次只能检查一个 - angular material two check boxes, only one can be checked at a time 仅在一个表中选中所有复选框 - Check all check boxes only in one table 有没有办法在选项(复选框)打开时单击多个复选框,而当它关闭时,您一次只能选择一个? - Is there way to click multiple checkboxes when a option(check box) is on and when it is off you can only select one at a time? 选中/取消选中所有复选框,单击一个复选框javascript - select/deselect all check boxes on click a single check box by javascript Jstree - 每次只允许检查所有 Jstree 中的一个节点 - Jstree - only allow to check one node each time in all the Jstree 根据复选框显示隐藏内容,如果选中一个复选框禁用另一个复选框 - Show hide content based on check boxes, if one check box checked disable another check boxes 如何在 React 的复选框列表中一次只选中一个复选框而不是所有复选框 - How can I check only one check box at a time instead of all in a list of checkboxes in React 如果未选中,则单击带有量角器的复选框 - click on check box with protractor only if not checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM