duanjuda5789 2015-05-08 20:55
浏览 302
已采纳

形成多个复选框,其中包含可以选择的数量的限制

I am having an issue with a form that I need to get the user to select a maximum of 2 checkbox and then it will then not let you select another one until you de-select one first I have got this working here https://jsfiddle.net/Ly1h825h/

But when the form is select I am not able to get the values for which ones have been ticked as the name of the checkbox are the same else the javascript will not work. So when I add the square brackets on the name so it becomes option[] this breaks the max limit on the checkbox you can select ie you can select all 4. which you can see in this example. https://jsfiddle.net/9tnwz8j2/

Can anyone see a way to fix this or something am doing wrong?

<form name="addtobasket50" id="addtobasket50" method="post" enctype="multipart/form-data" action="cart.php">
<label for="12345" id="select12345" class="inpSelect"><input type="checkbox" name="option[]" value="12345" id="12345" class="bundleChk" /> Select</label>
<label for="12346" id="select12346" class="inpSelect"><input type="checkbox" name="option[]" value="12346" id="12346" class="bundleChk" /> Select</label>
<label for="12347" id="select12347" class="inpSelect"><input type="checkbox" name="option[]" value="12347" id="12347" class="bundleChk" /> Select</label>
<label for="12348" id="select12348" class="inpSelect"><input type="checkbox" name="option[]" value="12348" id="12348" class="bundleChk" /> Select</label>
</form>
<script type="text/javascript">
function checkboxlimit(checkgroup, limit, bundleID){
    var checkgroup=checkgroup
    var limit=limit
    for (var i=0; i<checkgroup.length; i++){
        checkgroup[i].onclick=function(){
        var checkedcount=0
        for (var i=0; i<checkgroup.length; i++)
            checkedcount+=(checkgroup[i].checked)? 1 : 0
        if (checkedcount==limit) {
            document.getElementById('cart'+bundleID).disabled = false;
        }
        if (checkedcount>limit){
            //var prodID = this.checked.value;
            //alert(document.getElementsByName("option").checked);

            this.checked=false
            }
        }
    }
}
<script>
<script type="text/javascript">
checkboxlimit(document.forms.addtobasket50.option[], 2,50)
<script>

Thanks in advance Shuka

  • 写回答

1条回答 默认 最新

  • dongshang1768 2015-05-08 21:00
    关注

    When you use dot notation to access properties, the property name must be a valid identifier, but [] is not part of a valid identifier (brackets are used to index into arrays and objects). To refer to a property whose name is not an identifier, you have to use array notation instead, with the property name as a literal string, so it should be:

    checkboxlimit(document.forms.addtobasket50["option[]"], 2,50);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能