search

Home  >  Q&A  >  body text

javascript - How to abbreviate this jQuery function?

<!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>
高洛峰高洛峰2710 days ago784

reply all(8)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-26 10:56:41

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

    reply
    0
  • PHP中文网

    PHP中文网2017-06-26 10:56:41

    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();
    })

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-26 10:56:41

    <!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>

    reply
    0
  • typecho

    typecho2017-06-26 10:56:41

    $(function(){
       $.each($("input[name='purpose_id[]']"), function(index) {
            $(this).click(function(){
                if($(this).prop("checked")){
                    $(".bd").eq(index).show();
                }else{
                    $(".bd").eq(index).hide();
                }
            })
       });
    })

    reply
    0
  • 学习ing

    学习ing2017-06-26 10:56:41

    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>

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-26 10:56:41

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

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

    });

    reply
    0
  • 習慣沉默

    習慣沉默2017-06-26 10:56:41

    <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>

    reply
    0
  • 欧阳克

    欧阳克2017-06-26 10:56:41

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

    reply
    0
  • Cancelreply