怎么简写这段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>
阅读 4.5k
8 个回答
$(".purpose").change(function(){
            var index = $(this).index();
            if($(this).is(":checked")){
                $(".bd").eq(index).show();
            }else{
                 $(".bd").eq(index).hide();
            }
});

首先给bd的div加上data-v对应上面的value

<div class="bd" data-v="13">1放家庭装饰 input</div>
<div class="bd" data-v="17">2放礼物馈赠 input</div>

$('.purpose').on('click',function(){
    var $bd = $('div[data-v=' + $(this).val() + ']');
    if($bd.is(":hidden"))
        $bd.show();
    else
        $bd.hidden();
})
<!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="0">家庭装饰                                        
    <input type="checkbox" name="purpose_id[]" class="purpose" value="1">礼物馈赠   
    
    <div class="bd">1放家庭装饰 input</div>
    <div class="bd">2放礼物馈赠 input</div>
    
    <script>
        $(function(){
           $('.purpose:checkbox').on('click',function(e){
                   $('.bd:eq(' + e.target.value + ')').css('display', e.target.checked ? 'block' : 'none');
            })
        })
    </script>
</body>
</html>
$(function(){
   $.each($("input[name='purpose_id[]']"), function(index) {
        $(this).click(function(){
            if($(this).prop("checked")){
                $(".bd").eq(index).show();
            }else{
                $(".bd").eq(index).hide();
            }
        })
   });
})

div加个属性跟复选框能对应,那个通过index来进行判断的写法位置稍微一改动就没用

<input type="checkbox" name="purpose_id[]" class="purpose" value="13">家庭装饰
<input type="checkbox" name="purpose_id[]" class="purpose" value="17">礼物馈赠

<div class="bd" id="13">1放家庭装饰 input</div>
<div class="bd" id="17">2放礼物馈赠 input</div>

<script>
    $(function () {
        $('input[type=checkbox]').on('click', function () {
            var $div = $("#"+ $(this).val());
            $(this).attr("checked") ? $div.show() : $div.hide();
        })
    })
</script>

$(".purpose").on('change',function(){

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

});

<input type="checkbox" name="purpose_id[]" class="purpose" onchange="check1();" data="1" value="13">家庭装饰

<input type="checkbox" name="purpose_id[]" class="purpose" onchange="check2()" data="2" value="17">礼物馈赠

<div class="bd">1放家庭装饰 input</div>
<div class="bd">2放礼物馈赠 input</div>
<script>
    function check1(){
        $('.bd:eq(0)').toggle();
    }

    function check2(){
        $('.bd:eq(1)').toggle();
    }
</script>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题