input复选(checkbox):
<label>input复选1组:</label>
<input type="checkbox" name="checkbox1" value="checkbox复选1" checked="checked"/>checkbox复选1
<input type="checkbox" name="checkbox1" value="checkbox复选2"/>checkbox复选2
<input type="checkbox" name="checkbox1" value="checkbox复选3" checked="checked"/>checkbox复选3
相同name的单选项为同一组复选,checked="checked"选中某复选项;
1.checkbox选中项的值和索引(实际应该叫序号,index()的值从1开始,不是0)
<label>input复选2组:</label>
<input type="checkbox" name="checkbox2" value="checkbox复选1"/>checkbox复选1
<input type="checkbox" name="checkbox2" value="checkbox复选2" checked="checked"/>checkbox复选2
<input type="checkbox" name="checkbox2" value="checkbox复选3" checked="checked"/>checkbox复选3
$("input[name='checkbox2']:checked").val();//选中项的第一个值
$("input[name='checkbox2']:checked").each(function(){
alert("checkbox2组选中项的值:"+$(this).val());//遍历选中项的值
});
var index1 = $("input[name='checkbox2']:checked").index();//选中项的第一个序号
alert("checkbox2组选中项的项:"+index1);
$("input[name='checkbox2']:checked").each(function(){//遍历选中项的序号
alert("checkbox2组选中项的项:"+$(this).index());//遍历选中项的索引
});
2.checkbox值对应的索引和索引对应的值
<label>input复选3组:</label>
<input type="checkbox" name="checkbox3" value="checkbox复选1"/>checkbox复选1
<input type="checkbox" name="checkbox3" value="checkbox复选2"/>checkbox复选2
<input type="checkbox" name="checkbox3" value="checkbox复选3"/>checkbox复选3
checkbox索引对应的值:$("input[name='checkbox3']").eq().val();//checkbox复选3;eq(索引值),索引从0开始; checkbox值对应的索引:$("input[name='checkbox3'][value=checkbox复选2]").index();//2;index(序号),序号从1开始
$("input[name='checkbox3']:first").val();//checkbox第一项的值
$("input[name='checkbox3']:first").index();//checkbox第一项的索引
$("input[name='checkbox3']:last").val();//checkbox最后一项的值
$("input[name='checkbox3']:last").index();//checkbox最后一项的索引
3.checkbox选中和取消选中:
<label>input复选4组:</label>
<input type="checkbox" name="checkbox4" value="checkbox复选1"/>checkbox复选1
<input type="checkbox" name="checkbox4" value="checkbox复选2"/>checkbox复选2
<input type="checkbox" name="checkbox4" value="checkbox复选3"/>checkbox复选3
$("input[name='checkbox4'][value='checkbox复选1']").prop("checked",true);//选中某值对应的项
$("input[name='checkbox4'][value='checkbox复选1']").prop("checked",false);//取消选中某值对应的项
$("input[name='checkbox4'][value='checkbox复选2']").prop("checked","checked");//选中某值对应的项
$("input[name='checkbox4'][value='checkbox复选2']").removeProp("checked");//取消选中某值对应的项
$("input[name='checkbox4']").eq().prop("checked",true);//选中某索引对应的项
$("input[name='checkbox4']").eq().prop("checked",false);//取消选中某索引对应的项
$("input[name='checkbox4']").eq().prop("checked","checked");//选中某索引对应的项
$("input[name='checkbox4']").eq().removeProp("checked");//取消选中某索引对应的项
4.checkbox删除项:
<label>input复选5组:</label>
<input type="checkbox" name="checkbox5" value="checkbox复选1"/>checkbox复选1
<input type="checkbox" name="checkbox5" value="checkbox复选2"/>checkbox复选2
<input type="checkbox" name="checkbox5" value="checkbox复选3"/>checkbox复选3
$("input[name='checkbox5']").eq().remove();或者
$("input[name='checkbox5'][value=checkbox复选2]").remove(); 移除复选的项;
参考自:http://www.jb51.net/article/77946.htm
html内容:

<!DOCTYPE html>

<html lang="zh-CN">

<head>

  <meta charset="utf-8"/>

  <title>Form表单复选操作示例1</title>

  <style>

    body{font-size:14px;}

    label{display:inline-block;width:8em;margin-left:0.3em;margin-right:0.3em;}

input{margin-top:0.3em;margin-bottom:0.3em;}

.tipmsg{font-size:14px;color:#f00;}

  </style>

</head>

 

<body>

<form>

  <h2>input复选(checkbox):</h3>

  <div>

    <label>input复选1组:</label>

    <input type="checkbox" name="checkbox1" value="checkbox复选1" checked="checked"/>checkbox复选1

<input type="checkbox" name="checkbox1" value="checkbox复选2"/>checkbox复选2

<input type="checkbox" name="checkbox1" value="checkbox复选3" checked="checked"/>checkbox复选3

<span class="tipmsg">

相同name的单选项为同一组复选,checked="checked"选中某复选项;

</span>

  </div>

  

  <h3>checkbox选中项的值和索引(实际应该叫序号,index()的值从1开始,不是0)</h3><hr>

  <div>

    <label>input复选2组:</label>

    <input type="checkbox" name="checkbox2" value="checkbox复选1"/>checkbox复选1

<input type="checkbox" name="checkbox2" value="checkbox复选2" checked="checked"/>checkbox复选2

<input type="checkbox" name="checkbox2" value="checkbox复选3" checked="checked"/>checkbox复选3

<span class="tipmsg"><br>

$("input[name='checkbox2']:checked").val();//只返回选中项的第一个值<br>

each遍历获取多个选中项的值;<br>

$("input[name='checkbox2']:checked").val();//只返回选中项的第一个序号<br>

each遍历获取多个选中项的序号;<br>

</span>

  </div>

  

  <h3>checkbox值对应的索引和索引对应的值</h3><hr>

  <div>

    <label>input复选3组:</label>

    <input type="checkbox" name="checkbox3" value="checkbox复选1"/>checkbox复选1

<input type="checkbox" name="checkbox3" value="checkbox复选2"/>checkbox复选2

<input type="checkbox" name="checkbox3" value="checkbox复选3"/>checkbox复选3

<span class="tipmsg"><br>

$("input[name='checkbox3']").eq(2).val();//checkbox复选3;eq(索引值),索引从0开始<br>

$("input[name='checkbox3'][value=checkbox复选2]").index();//2;index(序号),序号从1开始<br>

$("input[name='checkbox3']:first").val();//checkbox第一项的值<br>

$("input[name='checkbox3']:first").index();//checkbox第一项的索引<br>

$("input[name='checkbox3']:last").val();//checkbox最后一项的值<br>

$("input[name='checkbox3']:last").index();//checkbox最后一项的索引

</span>

  </div>

  

  <h3>checkbox选中和取消选中</h3><hr>

  <div>

    <label>input复选4组:</label>

    <input type="checkbox" name="checkbox4" value="checkbox复选1"/>checkbox复选1

<input type="checkbox" name="checkbox4" value="checkbox复选2"/>checkbox复选2

<input type="checkbox" name="checkbox4" value="checkbox复选3"/>checkbox复选3

<span class="tipmsg"><br>

$("input[name='checkbox4'][value='checkbox复选1']").prop("checked",true);//选中某值对应的项<br>

$("input[name='checkbox4'][value='checkbox复选1']").prop("checked",false);//取消选中某值对应的项<br>

$("input[name='checkbox4'][value='checkbox复选2']").prop("checked","checked");//选中某值对应的项<br>

$("input[name='checkbox4'][value='checkbox复选2']").removeProp("checked");//取消选中某值对应的项<br>

 

$("input[name='checkbox4']").eq(1).prop("checked",true);//选中某索引对应的项<br>

$("input[name='checkbox4']").eq(1).prop("checked",false);//取消选中某索引对应的项<br>

$("input[name='checkbox4']").eq(2).prop("checked","checked");//选中某索引对应的项<br>

$("input[name='checkbox4']").eq(2).removeProp("checked");//取消选中某索引对应的项

</span>

  </div>

  

  <h3>checkbox删除项</h3><hr>

  <div>

    <label>input复选5组:</label>

    <input type="checkbox" name="checkbox5" value="checkbox复选1"/>checkbox复选1

<input type="checkbox" name="checkbox5" value="checkbox复选2"/>checkbox复选2

<input type="checkbox" name="checkbox5" value="checkbox复选3"/>checkbox复选3

<span class="tipmsg"><br>

 

</span>

  </div>

</form>

 

<script src="./jquery-1.x.min.js"></script>

<script>

$(function(){

  var val1 = $("input[name='checkbox2']:checked").val();//获取单个复选项的值;如果有多项选中,只返回所有选中项索引最小的值;

  //alert(val1);

  

  $("input[name='checkbox2']:checked").each(function(){

//alert("checkbox2组选中项的值:"+$(this).val());//遍历选中项的值

  });

  var index1 = $("input[name='checkbox2']:checked").index();

  //alert("checkbox2组选中项的项:"+index1);

  $("input[name='checkbox2']:checked").each(function(){

//alert("checkbox2组选中项的项:"+$(this).index());//遍历选中项的索引

  });

  

  var val2 = $("input[name='checkbox3']").eq(2).val();

  //alert("checkbox3索引2对应的值为:"+val2);//checkbox复选3(eq(索引值)索引值从0开始)

  var index2 = $("input[name='checkbox3'][value=checkbox复选2]").index();

  //alert("checkbox3值checkbox复选2对应的项为:"+index2);

  

  var var3 = $("input[name='checkbox3']:first").val();//checkbox第一项的值

  //alert(var3);

  var index3 = $("input[name='checkbox3']:first").index();//checkbox第一项的索引

  //alert(var3);

  //alert(index3);

  

  var var4 = $("input[name='checkbox3']:last").val();//checkbox最后一项的值

  //alert(var4);

  var index4 = $("input[name='checkbox3']:last").index();//checkbox最后一项的索引

  //alert(index4);

  

  //$("input[name='checkbox4'][value='checkbox复选1']").prop("checked",true);//选中某值对应的项

  //$("input[name='checkbox4'][value='checkbox复选1']").prop("checked",false);//取消选中某值对应的项

  //$("input[name='checkbox4'][value='checkbox复选2']").prop("checked","checked");//选中某值对应的项

  //$("input[name='checkbox4'][value='checkbox复选2']").removeProp("checked");//取消选中某值对应的项

  

  $("input[name='checkbox4']").eq(1).prop("checked",true);//选中某索引对应的项

  $("input[name='checkbox4']").eq(1).prop("checked",false);//取消选中某索引对应的项

  $("input[name='checkbox4']").eq(2).prop("checked","checked");//选中某索引对应的项

  $("input[name='checkbox4']").eq(2).removeProp("checked");//取消选中某索引对应的项

  

  //$("input[name='checkbox5']").eq(1).remove();

  $("input[name='checkbox5'][value=checkbox复选2]").remove();

});

</script>

</body>

</html>
 
原文:https://blog.csdn.net/qinshijangshan/article/details/54408004?utm_source=copy 

Form表单之复选框checkbox操作的更多相关文章

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:复选框(Checkbox)和单选框(Radio)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. DOM表单(复选框)

    在表单中,尤为重要的一个属性是name <!--复选框的全选.全不选.反选--> <!DOCTYPE> <html> <head lang="en& ...

  3. 表单提交复选框(checkbox)注意事项

    例子: <form action="a.php" method="post"> <input type="checkbox" ...

  4. 及时从数据库中取得数据填放进Form表单的多选框中

    #写上以下代码就不用担心数据库添加了数据而不能及时获取了 def __init__(self, *args, **kwargs): #每次创建Form1对象时执行init方法 super(Form1, ...

  5. 关于bootstrap--表单(下拉<select>、输入框<input>、文本域<textare>复选框<checkbox>和单选按钮<radio>)

    html 里面的 role 本质上是增强语义性,当现有的HTML标签不能充分表达语义性的时候,就可以借助role来说明.通常这种情况出现在一些自定义的组件上,这样可增强组件的可访问性.可用性和可交互性 ...

  6. php 判断复选框checkbox是否被选中

    php 判断复选框checkbox是否被选中   复选框checkbox在php表单提交中经常被使用到,本文章通过实例向大家介绍php如何判断复选框checkbox中的值是否被选中,需要的朋友可以参考 ...

  7. layui 添加复选框checkbox后,无法正确显示及点击的方法

    layui 添加复选框checkbox后,无法正确显示方式,这个是由于html里的样式添加 layui-form后,没有加载 form插件 ,具体如下: <body style="ba ...

  8. [原创]纯JS实现网页中多选复选框checkbox和单选radio的美化效果

    图片素材: 最终效果图: <html><title> 纯JS实现网页中多选复选框checkbox和单选radio的美化效果</title><head>& ...

  9. 使用CSS3美化复选框checkbox

    我们知道HTML默认的复选框样式十分简陋,而以图片代替复选框的美化方式会给页面表单的处理带来麻烦,那么本文将结合实例带您一起了解一下使用CSS3将复选框checkbox进行样式美化,并且带上超酷的滑动 ...

随机推荐

  1. !function()是干什么的?

    叹号后面跟函数!function和加号后面跟函数+function都是跟(function(){})();这个函数是一个意思,都是告诉浏览器自动运行这个匿名函数的,因为!+()这些符号的运算符是最高的 ...

  2. vue——介绍和使用

    一.vue介绍 vue官网说:Vue.js(读音 /vjuː/,类似于 view) 是一套构建用户界面的渐进式的JavaScript框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计. ...

  3. grunt 常用插件有哪些?

    作者:顾城链接:https://www.zhihu.com/question/21917526/answer/19747259来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  4. 详细分析UserLock如何保证高校内部信息安全

    俄克拉荷马城市公立学校的IT团队负责该片区接近43000个学生的网络管理工作.长期以来,学生和教师员工共享Windows网络登录为他们带来了很多难题. 由于没有并发登录的限制,也不能对网络使用情况进行 ...

  5. Catia 二次开发资料(转)

    Catia 二次开发 CATIA V5在开发之初就遵循面向对象的设计思想(OO),构建了完全基于组件的体系结构(PPR: Products,Process,Resource),有效地解决了维护.管理. ...

  6. asyncio标准库5 TCP echo client and server

    server import asyncio async def handle_echo(reader, writer): data = await reader.read(100) message = ...

  7. 【Spring实战】—— 1 入门讲解

    这个系列是学习spring实战的总结,一方面总结书中所写的精髓,另一方面总结一下自己的感想. 基础部分讲解了spring最为熟知的几个功能:依赖注入/控制反转 和 面向切面编程. 这两个就不再多说了, ...

  8. Apache轻量级性能測试工具--ab

    Apache轻量级性能測试工具--ab ab早已不是什么新奇玩意,平时工作中会须要一些性能測试.简单的性能測试全然能够由AB来替代,而不须要动用LR这样重量级的工具. 此文简介一下ab的工具使用与结果 ...

  9. Python:运算与循环

    1.格式化输出 name = input("请输入你的名字:") age =input("请输入你的年龄:") job =input("请输入你的工作 ...

  10. 2018_MCM_ICM_C