javascript - 怎么简写这段jQuery功能?

查看:79
本文介绍了javascript - 怎么简写这段jQuery功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <style>
        .bd {display: none;}
    </style>
</head>
<body>
    
    
    <input type="checkbox" name="purpose_id[]" class="purpose" value="13">家庭装饰                                        
    <input type="checkbox" name="purpose_id[]" class="purpose" value="17">礼物馈赠   
    
    <div class="bd">1放家庭装饰 input</div>
    <div class="bd">2放礼物馈赠 input</div>
    
    <script>
        $(function(){
            var onff0 = true;
            var onff1 = true;
           $('.purpose:eq(0):checkbox').on('click',function(){
                if (onff0) {
                    $('.bd:eq(0)').css('display','block');
                } else {
                    $('.bd:eq(0)').css('display','none');
                }
                onff0 = !onff0;
            })
           $('.purpose:eq(1):checkbox').on('click',function(){
                if (onff1) {
                    $('.bd:eq(1)').css('display','block');
                } else {
                    $('.bd:eq(1)').css('display','none');
                }
                onff1 = !onff1;
            })
        })
    </script>
    
    
    
   
</body>
</html>

解决方案

$(".purpose").change(function(){
            var index = $(this).index();
            if($(this).is(":checked")){
                $(".bd").eq(index).show();
            }else{
                 $(".bd").eq(index).hide();
            }
});

这篇关于javascript - 怎么简写这段jQuery功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆