• JQuery操作checkbox、radio


    例:将多个选中的checkbox的值组装成一个字符串

    <script type=text/javascript>
    function addMem(){
    //var followers = document.getElementsByName("followers");
    var f_str = '0';
    $("input[@name='followers']").each(function(){
       if($(this).attr("checked")==true){
        f_str += ","+$(this).attr("value");
       }
    })
    alert(f_str);
    }
    </script>

    =====================

    例:取选中的radio的值

    var gender = $('input[@name=gender][@checked]').val();

    =====================

    转别人的一些东西:

    jquery判断checkbox是否被选中

    在html的checkbox里,选中的话会有属性checked="checked"。

    如果用一个checkbox被选中,alert这个checkbox的属性"checked"的值alert($"#xxx".attr("checked")),会打印出"true",而不是"checked"!

    如果没被选中,打印出的是"undefined"。觉得很奇怪是吗?继续看下去~

    不要尝试去做这样的判断:if($"#xxx".attr("checked")=="true")

    因为这么做是错的,jQuery的API手册上写,attr(name)的返回值是object。

    所以,应该是if($"#xxx".attr("checked")==true)

    ====================================

    jquery全选/取消选择checkbox示例:

    <input type="checkbox" name="checkbox_name[]” id=”checkbox_name_1″ />1<br />
    <input type=”checkbox” name=”checkbox_name[]” id=”checkbox_name_2″ />2<br />
    <input type=”checkbox” name=”checkbox_name[]” id=”checkbox_name_3″ />3<br />
    <input type=”checkbox” name=”checkbox_name[]” id=”checkbox_name_4″ />4<br />
    <input type=”checkbox” name=”checkedAll” id=”checkedAll”/>全选/取消全选

    1. <script type="text/javascript">
    2. <!--
    3. $(function() {
    4. $("#checkedAll").click(function() {
    5. if ($(this).attr("checked") == true) { // 全选
    6.    $("input[@name='checkbox_name[]']").each(function() {
    7.    $(this).attr("checked", true);
    8.   });
    9. } else { // 取消全选
    10.    $("input[@name='checkbox_name[]']").each(function() {
    11.    $(this).attr("checked", false);
    12.   });
    13. }
    14. });
    15. });
    16. //-->
    17. </script>

    =================================================

    jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关

    获取一组radio被选中项的值
    var item = $('input[@name=items][@checked]').val();
    获取select被选中项的文本
    var item = $("select[@name=items] option[@selected]").text();
    select下拉框的第二个元素为当前选中值
    $('#select_id')[0].selectedIndex = 1;
    radio单选组的第二个元素为当前选中值
    $('input[@name=items]').get(1).checked = true;

    获取值:

    文本框,文本区域:$("#txt").attr("value");
    多选框checkbox:$("#checkbox_id").attr("value");
    单选组radio:   $("input[@type=radio][@checked]").val();
    下拉框select: $('#sel').val();

    控制表单元素:
    文本框,文本区域:$("#txt").attr("value",'');//清空内容
                     $("#txt").attr("value",'11');//填充内容

    多选框checkbox: $("#chk1").attr("checked",'');//不打勾
                     $("#chk2").attr("checked",true);//打勾
                     if($("#chk1").attr('checked')==undefined) //判断是否已经打勾

    单选组radio:    $("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项
    下拉框select:   $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项
                    $("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//添加下拉框的option
                    $("#sel").empty();//清空下拉框

     

    谁都知道 在html 如果一个复选框被选中 是 checked="checked"。

    但是我们如果用jquery alert($("#id").attr("checked")) 会提示您是true而不是checked

    所以很多朋友判断  if($("#id").attr("checked")=="true") 这个是错误的,其实应该是 if($("#id").attr("checked")==true)

    例子里面包括了一下几个功能。

       <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="获得选中的所有值">

    代码

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <SCRIPT LANGUAGE="JavaScript" src="http://www.cnjquery.com/demo/jquery.js%22%3E%3C/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()+""r"n";
       //alert($(this).val());
        })
       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">
       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>

    /************单个checkbox全选************************/

    function clickCheckbox() {
               if($("#checkPathAll").attr("checked"))
               {
                 $("input[name='checkPath']").each(function() {
                     $(this).attr("checked", true);
                 });
               }
               else
               {
                 $("input[name='checkPath']").each(function() {
                     $(this).attr("checked", false);
                 });         
               }
            }

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kinglino520/archive/2009/11/03/4763608.aspx

    jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关

    获取一组radio被选中项的值
    var item = $('input[@name=items][@checked]').val();
    获取select被选中项的文本
    var item = $("select[@name=items] option[@selected]").text();
    select下拉框的第二个元素为当前选中值
    $('#select_id')[0].selectedIndex = 1;
    radio单选组的第二个元素为当前选中值
    $('input[@name=items]').get(1).checked = true;

    获取值:

    文本框,文本区域:$("#txt").attr("value");
    多选框checkbox:$("#checkbox_id").attr("value");
    单选组radio:   $("input[@type=radio][@checked]").val();
    下拉框select: $('#sel').val();

    控制表单元素:
    文本框,文本区域:$("#txt").attr("value",'');//清空内容
                     $("#txt").attr("value",'11');//填充内容

    多选框checkbox: $("#chk1").attr("checked",'');//不打勾
                     $("#chk2").attr("checked",true);//打勾
                     if($("#chk1").attr('checked')==undefined) //判断是否已经打勾

    单选组radio:    $("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项
    下拉框select:   $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项
                    $("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//添加下拉框的option
                    $("#sel").empty();//清空下拉框

  • 相关阅读:
    资源汇总
    最近面试经历
    Screen Space Ambient Occlusion
    实时渲染下的多线程协作:实践
    High Dynamic Range
    Deferred Shading
    实时渲染下的多线程协作
    9.了解四个相等判断的用法
    14.使用构造函数链
    16.垃圾最小化
  • 原文地址:https://www.cnblogs.com/aqbyygyyga/p/2012987.html
Copyright © 2020-2023  润新知