javascript - How to abbreviate this jQuery function?
高洛峰
高洛峰 2017-06-26 10:54:41
0
8
783
<!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">礼物馈赠   
    
    <p class="bd">1放家庭装饰 input</p>
    <p class="bd">2放礼物馈赠 input</p>
    
    <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>
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(8)
我想大声告诉你
$(".purpose").change(function(){
            var index = $(this).index();
            if($(this).is(":checked")){
                $(".bd").eq(index).show();
            }else{
                 $(".bd").eq(index).hide();
            }
});
洪涛

First add data-v to p of bd corresponding to the value above

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

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

p Adding an attribute can correspond to the check box. The writing method of judging by index will be useless if the position is slightly changed

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

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

<script>
    $(function () {
        $('input[type=checkbox]').on('click', function () {
            var $p = $("#"+ $(this).val());
            $(this).attr("checked") ? $p.show() : $p.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">Home decoration

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

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

    function check2(){
        $('.bd:eq(1)').toggle();
    }
</script>
学霸

What function are you trying to use? I don’t understand. . . . . .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template