Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26267461
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: 系统运维

2010-03-20 09:19:16

JQuery操作checkbox整理

[整理人:遥方 整理时间:2010-3-20]

一、如何判断一个checkbox是否选择上?

if($("#aa").attr("checked")==true) {

    alert("checkbox勾选择上");

}

二、 常用的操作请看整理的代码

HTML PUBLIC "-//W3C//DTD HTML 4.01//ZH-CN" "">

<html>

    <head>

       <meta http-equiv="Content-Type" content="text/html">

           <script type="text/javascript" src="jquery-1.3.2.min.js">script>

             <SCRIPT LANGUAGE="JavaScript">

       $("document").ready(function(){

            $("#btn1").click(function(){

            $("[name='checkbox']").attr("checked",'true');//全选

       

        })

           $("#btn2").click(function(){

             $("[name='checkbox']").removeAttr("checked");//取消全选

         })

        $("#btn3").click(function(){

          $("[name='checkbox']:even").attr("checked",'true');//选中所有奇数

         })

        $("#btn4").click(function(){

        

        $("[name='checkbox']").each(function(){

        

      

         if($(this).attr("checked"))

       {

        $(this).removeAttr("checked");

       

       }

       else

       {

        $(this).attr("checked",'true');

       

       }

      

        })

     

        })

         $("#btn5").click(function(){

        var str="";

        $("[name='checkbox'][checked]").each(function(){

         str+=$(this).val()+"\n";

        })

       alert(str);

        })

       })

       script>

 

    head>

     <BODY>

 <form name="form1" method="post" action="">

   <input type="button" id="btn1" value="全选">

   <input type="button" id="btn2" value="取消全选">

   <input type="button" id="btn3" value="选中所有奇数">

   <input type="button" id="btn4" value="反选">

   <input type="button" id="btn5" value="获得选中的所有值">

   <br>

   <input type="checkbox" name="checkbox" value="checkbox1" id="aa">

   checkbox1

   <input type="checkbox" name="checkbox" value="checkbox2">

   checkbox2

   <input type="checkbox" name="checkbox" value="checkbox3">

   checkbox3

   <input type="checkbox" name="checkbox" value="checkbox4">

   checkbox4

   <input type="checkbox" name="checkbox" value="checkbox5">

   checkbox5

   <input type="checkbox" name="checkbox" value="checkbox6">

   checkbox6

   <input type="checkbox" name="checkbox" value="checkbox7">

   checkbox7

   <input type="checkbox" name="checkbox" value="checkbox8">

 checkbox8

 form>

 body>

 html>

 

非常方便!




----------------版本02补充

JQuery 获取checkbox的值然后 ajax提交

        //服务器列表
        var str="";
        $("[name='host[]'][checked]").each(function(){
             str+=$(this).val()+"\n";
        })
        if(str == "") {
            alert("请选择服务器");
            return;
        }
        var selectedItems = new Array();
     $("input[@name='host[]']:checked").each(function() {selectedItems.push($(this).val());});
        if (selectedItems .length == 0) {
            alert("Please select item(s) to delete.");
        }
        var host = selectedItems.join('|');




$.ajax({
            url: '',
            type: 'post',
            async: false,           //如果服务器要开一个exec记得加这个参数值。要不然返回http 0!
            dataType: 'text',
            data:{"idc":idcname,"host":host},
            timeout: 1000,
            error: function(){
                alert('Error loading XML document');
                $("#uploadresult").html("");
            },
            success: function(xml){
                $("#uploadresult").show();
                $("#upload").toggle();
                $("#uploadresult").html(xml);
            }

        });

然后服务器获取:

$res=explode("|",$host);

for($i=0;$i         $cmd = $cmd . "  --host " . $res[$i]."  ";   
    }

就行了!



===========================补充一段AJAX==========================================


$("document").ready(function(){
    $(".classinfo").click(function(){
        var self = this;                   //闭包的一些概念!
        var obj = this;
        if (obj.checked == true) {
            //表示要对它进行配置了
        } else {
            if(window.confirm("你确定要将此类移除?")) {
                //通过ajax将这个类的属性配置删除掉
                var classid = obj.value;
                var hostgroupid  = $("#groupid").val();
                $.ajax({
                    type:"GET",
                    url: "?a=puppet&m=hostgroupclassdel&classid="+classid+"&hostgroupid="+hostgroupid+"&num="+Math.random(),  
                    dataType: 'text', 
                    success:function(msg){
                        if(msg=='1') {
                            alert("OK");
                            $(self).attr("disabled","disabled");
                        } else {
                            alert("DOWN");
                        }
                    }
                })
            }else {
                $(this).attr("checked",true);   //如果用户不选择
            }
        }
    });






阅读(2003) | 评论(6) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-07-17 17:04:20

使用JQuery修改checkbox的值的方法: $("#btype"+tid).attr("checked",''); $("#btype"+tid).val("0");还是使用val的方法进行操作的!

chinaunix网友2010-05-30 15:48:00

如何通过Jquery简单又快速的获取一组radio的取值呢? Jquery老的版本 var_name = $(“input[@name='radio_name']:checked”).val(); Jquery 1.3以后的版本 var_name = $(“input[name='radio_name']:checked”).val(); 区别是老版本有个@.

hkebao2010-05-12 16:50:30

$("#config_draw"+userid).attr("disabled","disabled"); 控制checkbox的方法

chinaunix网友2010-05-12 16:38:22

function toggleStatus() { if ($('#toggleElement').is(':checked')) { $('#elementsToOperateOn :input').attr('disabled', true); } else { $('#elementsToOperateOn :input').removeAttr('disabled'); } }

chinaunix网友2010-05-10 17:43:08

操作radio的相关操作 1、获取当前radio选中的数据 var config_order = $("input[@name=config_order"+userid+"][@checked]").val(); 2、设置radio的属性。因为它没有 readonly属性所以只能使用disabled属性 3、动态修改属性 $("input[@type=radio]").attr("disabled","disabled"); $("input[@type=radio]").attr("disabled","");