栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

html在组中仅选择一个复选框

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

html在组中仅选择一个复选框

该代码段将:

  • 允许像单选按钮一样分组
  • 像电台一样
  • 允许取消选择所有
// the selector will match all input controls of type :checkbox// and attach a click event handler $("input:checkbox").on('click', function() {  // in the handler, 'this' refers to the box clicked on  var $box = $(this);  if ($box.is(":checked")) {    // the name of the box is retrieved using the .attr() method    // as it is assumed and expected to be immutable    var group = "input:checkbox[name='" + $box.attr("name") + "']";    // the checked state of the group/box on the other hand will change    // and the current value is retrieved using .prop() method    $(group).prop("checked", false);    $box.prop("checked", true);  } else {    $box.prop("checked", false);  }});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><div>  <h3>Fruits</h3>  <label>    <input type="checkbox"  value="1" name="fooby[1][]" />Kiwi</label>  <label>    <input type="checkbox"  value="1" name="fooby[1][]" />Jackfruit</label>  <label>    <input type="checkbox"  value="1" name="fooby[1][]" />Mango</label></div><div>  <h3>Animals</h3>  <label>    <input type="checkbox"  value="1" name="fooby[2][]" />Tiger</label>  <label>    <input type="checkbox"  value="1" name="fooby[2][]" />Sloth</label>  <label>    <input type="checkbox"  value="1" name="fooby[2][]" />Cheetah</label></div>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/464868.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号