• jquery :checked(过滤选择器) 和 空格:checked(后代选择器)【转】


    jquery 过滤选择器 和 后代选择器

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ include file="/WEB-INF/jsp/common/base.jsp"%>
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <jsp:include page="/WEB-INF/jsp/common/baseJS.jsp"></jsp:include>
        <jsp:include page="/WEB-INF/jsp/common/baseCSS.jsp"></jsp:include>
    <title>checkbox&nbsp;radio.jsp</title>
    </head>
    <body>
        <form action="">
            <input type="radio" name="sex" value="male" checked="checked"/> Male
            <input type="radio" name="sex" value="female" /> Female
            <hr/>
            I have a bike:
            <input type="checkbox" name="vehicle" value="Bike" />
            <br />
            I have a car:
            <input type="checkbox" name="vehicle" value="Car" checked="checked" />
            <br />
            I have an airplane:
            <input type="checkbox" name="vehicle" value="Airplane" />
        </form>
        <button class="radio">radio</button>
        <button class="checkbox">checkbox</button>
        <button class="all">__all__</button>
        
        <script type="text/javascript">
            $(function(){
                $(".radio").click(function() {
                    //form:checked一定不要加空格,意为过滤选择器,即选取:checked的input[name='sex']的元素
                    var $checkedObj = $("input[name='sex']:checked");
                    console.log("被选中的radio共有"+ $checkedObj.size() +"");
                    $.each( $checkedObj , function(index,obj){
                        alert(""+index+"个被选中的radio的值为 "+$(obj).val());
                    });
                });
                
                
                $(".checkbox").click(function() {
                    //form:checked一定不要加空格,意为过滤选择器,即选取:checked的input[name='vehicle']的元素
                    var $checkedObj = $("input[name='vehicle']:checked");
                    console.log("被选中的checkbox共有"+ $checkedObj.size() +"");
                    $.each($checkedObj,function(index,obj){
                        alert(""+index+"个被选中的checkbox的值为 "+$(obj).val());
                    });
                });
                
                
                $(".all").click(function() {
                    //form :checked一定要加空格,意为后代选择器,即form的内容中带有:checked的元素
                    var $checkedObj = $("form :checked");
                    console.log("被选中的元素共有"+ $checkedObj.size() +"");
                    $.each($checkedObj,function(index,obj){
                        alert(""+index+"个被选中的元素的值为 "+$(obj).val());
                    });
                });
            })
        </script>
    </body>
    </html>

    补充说明 $.each(list,function(index,obj)){...}用法

    http://www.cnblogs.com/cdemo/p/4142695.html

    $.each可以遍历element dom元素正如上例中用法,也可以遍历jsonArray如下.

    //如果myJsonArray = [{ id: 1, department: '开发部' }, { id: 2, department: '销售部' }, {id:3,department:'行政部'}];
     $.each(myJsonArray,function(index,obj){
        alert("第"+index+"个被选中的元素的值为 "+obj.department);
    });
  • 相关阅读:
    数列分块入门九题(一):LOJ6277~6279
    Luogu P4211 [LNOI2014]LCA
    Luogu P2279 [HNOI2003]消防局的设立
    Luogu P3177 [HAOI2015]树上染色
    51Nod 1677 treecnt
    CYJian的水题大赛
    51Nod 1299 监狱逃离
    51Nod 1705 七星剑
    51Nod 1443 路径和树
    51Nod 1815 调查任务
  • 原文地址:https://www.cnblogs.com/whatlonelytear/p/4748276.html
Copyright © 2020-2023  润新知