Jquery if checkbox is checked add a class

前端 未结 7 2145
梦如初夏
梦如初夏 2020-12-06 01:17

I am trying to add a class when a checkbox is checked.

My jquery:

$(\'input\').attr(\"checked\").change(function(){
$(\'div.menuitem\').addClass(\"me         


        
相关标签:
7条回答
  • 2020-12-06 02:02

    $('.vfb-checkbox input:checkbox').change(function(){
         if($(this).is(":checked")) {
            $('.vfb-checkbox label').addClass("checked");
        } else {
            $('.vfb-checkbox label').removeClass("checked");
        }
    });
    .vfb-checkbox{
      width:180px;
      height:130px;
     background:url('http://i304.photobucket.com/albums/nn181/Amam_Dewan/2932337756_1845773ed4_q_d_zpsea5idhnt.jpg');
    }
    /* checkboxes */
    
    .vfb-checkbox label {
    		  cursor: pointer;
    	    display: block;
    	    position: relative;
      width:100%;
      height:100%;
    
    	}
    
    .vfb-checkbox label:hover{
      background:rgba(0,0,0,.7) url(http://i304.photobucket.com/albums/nn181/Amam_Dewan/selected_zpsvfoaira0.png)center center no-repeat;
    }
    
    	
    .vfb-checkbox input[type=checkbox] {  
        	display: none;
    	}
    	
    .checked{
      	    background: rgba(0,0,0,.4) url('http://i304.photobucket.com/albums/nn181/Amam_Dewan/selected_zpsvfoaira0.png') center center no-repeat;  
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="vfb-checkbox">
      <label id="checkbox" for="check1">
        <input id="check1" type="checkbox" name="check" value="check1">
      </label>
    </div>
    <div class="vfb-checkbox">
      <label id="checkbox" for="check1">
        <input id="check1" type="checkbox" name="check" value="check1">
      </label>
    </div>

    I used jquery in checkbox. When I clicked on one element the other elements are effected. How can I fix this problem?

    0 讨论(0)
提交回复
热议问题