dongzheng3113 2016-02-03 01:51
浏览 14
已采纳

限制涉及数组时检查的框数

I'm pretty amateurish when it comes to JavaScript stuff, so I apologize if this question comes off as a bit dumb.

I'm currently trying to code something that involves forms with a limit on how many checkboxes can be selected. The method I've come across that has worked best for my purposes so far is the one detailed here:

http://www.javascriptkit.com/script/script2/checkboxlimit.shtml

It works for the most part but I run into an issue when checkboxes need to output an array. For example, if I write the input line as:

<input type="checkbox" name="choice[]" value="One" /> One<br />
<input type="checkbox" name="choice[]" value="Two" /> Two<br />
<input type="checkbox" name="choice[]" value="Three" /> Three<br />

I've tried quite a few things but I can't figure out how to change the code so that it works with the brackets in the input's name field.

  • 写回答

3条回答 默认 最新

  • dongyan3853 2016-02-03 02:06
    关注

    Using that script above, you can change it into using .elements to get the group of elements and applying that same custom function:

    <script type="text/javascript">
    
    // Syntax: checkboxlimit(checkbox_reference, limit)
    
    var world_form = document.forms.choices; // the form
    var my_checkboxes = world_form.elements['choice[]']; // check boxes
    checkboxlimit(my_checkboxes, 2);
    
    </script>
    

    Fiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器