JQuery总结-radioButton、checkBox相关

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">

        function t() {
            //获得所有单选按钮   且属性name=gender的  且是选中的
            alert($(":radio[name=gender]:checked").val());
        }


        function t1() {
            //注意  val中必须是[]    
            //给所有单选按钮 且属性是name=gender的设置value属性
            $(":radio[name=gender]").val(["男"]);
        }

        function t2() {
            //注意  val中必须是[]  给value=a和b的checkbox选中
            $(":checkbox").val(["a", "b"]);
        }

        function t3() {
            //将div1中所有checkbox设置选中
            $("#div1 input[type=checkbox]").attr("checked", true);
        }


        function t4() {
            //将div1中所有checkbox设置不选中
            $("#div1 :checkbox").attr("checked", false);
        }


        function t5() {
            //遍历div1中所有的checkbox
            $("#div1 input[type=checkbox]").each(function () {
                //设置当前Checkbox的checked属性为当前的反
                $(this).attr("checked", !$(this).attr("checked"));
            }
            );
        }

        function t6() {
            var tmp1 = $("#div1 input[type=checkbox]:checked");
            var tmp2 = $("#div1 input[type=checkbox][checked=false]");
            tmp1.attr("checked", false);
            tmp2.attr("checked", true);
        }

    </script>
</head>
<body>
    <input type="radio" value="男" name="gender" />男
    <input type="radio" value="女" name="gender" />女
    <br />
    <button οnclick="t()">
        获取选中的value</button><br />
    <br />
    <button οnclick="t1()">
        设置选中的radiobutton(通过设置value的值 来决定哪个选中)</button>
    <hr />
    <input type="checkbox" value="a" />a
    <input type="checkbox" value="b" />b
    <input type="checkbox" value="c" />c
    <input type="checkbox" value="d" />d<br />
    <button οnclick="t2()">
        设置选中的checkbox(通过设置value的值 来决定哪个选中)</button>
    <hr />
    <p>
        也可单独通过$(xxx).att("checked",true)来设置选中</p>
    <hr />
    <div id="div1">
        <input type="checkbox" value="a" />a
        <input type="checkbox" value="b" />b
        <input type="checkbox" value="c" />c
        <input type="checkbox" value="d" />d<br />
    </div>
    <button οnclick="t3()">
        全选(注意过滤器前有个空格)</button>
    <button οnclick="t4()">
        全不选</button>
    <button οnclick="t5()">
        反选1</button>
    <button οnclick="t6()">
        反选2</button>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值