首页 技术 正文
技术 2022年11月13日
0 收藏 606 点赞 3,155 浏览 2163 个字
var id_array=new Array();
$('input[name="id"]:checked').each(function(){
id_array.push($(this).attr('id'));//向数组中添加元素
});
var idstr=id_array.join(',');//将数组元素连接起来以构建一个字符串
alert(idstr);

JQuery对CheckBox的一些相关操作

一、通过选择器选取CheckBox:

1.给CheckBox设置一个id属性,通过id选择器选取:

<input type="checkbox" name="myBox" id="chkOne" value="1" checked="checked" />

JQuery:

        $("#chkOne").click(function(){}); 

2.给CheckBox设置一个class属性,通过类选择器选取:

<input type="checkbox" name="myBox" class="chkTwo" value="1" checked=”checked” />

JQuery:

        $(".chkTwo").click(function(){});   3.通过标签选择器和属性选择器来选取:   <input type="checkbox" name="someBox"  value="1" checked="checked" />

<input type="checkbox" name="someBox" value="2" />

   JQuery:

        $("input[name='someBox']").click(function(){}); 二、对CheckBox的操作:   以这段checkBox代码为例:    <input type="checkbox" name="box"  value="0" checked="checked" />

<input type="checkbox" name="box" value="1" />

   <input type="checkbox" name="box" value="2" />

   <input type="checkbox" name="box" value="3" />

    1.遍历checkbox用each()方法:       $("input[name='box']").each(function(){});
   2.设置checkbox被选中用attr();方法:     $("input[name='box']").attr("checked","checked");  

在HTML中,如果一个复选框被选中,对应的标记为 checked=”checked”。 但如果用jquery alert($(“#id”).attr(“checked”)) 则会提示您是”true”而不是”checked”,所以判断 if(“checked”==$(“#id”).attr(“checked”)) 是错误的,应该是 if(true == $(“#id”).attr(“checked”))

   3.获取被选中的checkbox的值:    $(“input[name=’box’][checked]”).each(function(){
    if (true == $(this).attr(“checked”)) {
          alert( $(this).attr(‘value’) );
    }     或者:    $(“input[name=’box’]:checked”).each(function(){
    if (true == $(this).attr(“checked”)) {
          alert( $(this).attr(‘value’) );
    }
   $(“input[name=’box’]:checked”)与 $(“input[name=’box’]”)有何区别没试过,我试了用 $(“input[name=’box’]”)能成功。

4.获取未选中的checkbox的值:

$(“input[name=’box’]”).each(function(){
          if ($(this).attr(‘checked’) ==false) {
                alert($(this).val());
            }
     });

5.设置checkbox的value属性的值:

$(this).attr(“value”,值);

三、 一般都是创建一个js数组来存储遍历checkbox得到的值,创建js数组的方法:

1.  var array= new Array();

2. 往数组添加数据:

array.push($(this).val());

3.数组以“,”分隔输出:

alert(array.join(‘,’));

    

<input type="checkbox" name="myBox" class="chkTwo" value="2" />

<input type="checkbox" name="myBox" id="chkOne" value="2" />

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,500
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,914
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,747
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,504
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,142
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,306