简体   繁体   English

检查选中了哪些复选框

[英]Check which checkboxes is checked

I have read this post and this post and others but i didnt get it to work in my code. 我已经阅读了这篇文章这篇文章以及其他文章 ,但是我没有在我的代码中使用它。

here is the Fiddle for better see my code on live. 这是小提琴 ,可以更好地实时查看我的代码。

this is my js 这是我的js

 //////first code to try////
 if ($('input.chek').is(':checked')) {
      $('.check-addon').css('background-color','green');
   }

  //////second code///////
var boxes = $('input[class="chek"]:checked');
       $(boxes).each(function(){
       $('.check-addon').css('background-color','green');
 });

this is my html 这是我的HTML

   <div class="inline-container"><div class="checkboxes" >
        <span class="check-addon"><input id="id1" class="chek" type="checkbox" value="1" name="id1">title1</span>
        <span class="check-addon"><input id="id2" type="checkbox" class="chek" value="1" name="id2">title2</span>
        <span class="check-addon"><input id="id3" type="checkbox" class="chek" value="1" name="id3">title3</span>
   </div></div>

i dont know what im doing wrong here . 我不知道我在这里做错了什么。 i couldnt get the background Green Of the checked box in my code. 我无法在我的代码中获得选中的复选框的绿色背景。

any help would much apreciated. 任何帮助将不胜感激。

EDIT even the answers down works in fiddle But i guess i have more problem here. 编辑甚至答案都在小提琴中起作用,但我想我在这里还有更多问题。

if i write html code above Normal , the js code is fired and works But im using html code inside js function like that: 如果我在Normal之上编写html代码,则js代码会被触发并起作用,但是我在js函数中使用html代码是这样的:

  function houses(){
            var x ='<div class="inline-container"><div class="checkboxes" >
        <span class="check-addon"><input id="id1" class="chek" type="checkbox" value="1" name="id1">title1</span>
        <span class="check-addon"><input id="id2" type="checkbox" class="chek" value="1" name="id2">title2</span>
        <span class="check-addon"><input id="id3" type="checkbox" class="chek" value="1" name="id3">title3</span>
   </div></div>';
   return x ;
  }

so this function when i call it it works but when i want apply the above code on it its not working and not firing at all. 因此,当我调用此函数时,此函数有效,但是当我要将上面的代码应用于其上时,该函数将不起作用且完全不会触发。

this function is Out of the Dom handler. 此函数不在Dom处理程序中。 Fiddle here 在这里摆弄

Bind the input with a on change event 将输入与on change事件绑定

$('.chek').on('change', function () {
    $(this).closest('.check-addon').css('background-color',this.checked?'green':'none');
});

DEMO 演示

Update 更新资料

You need to use event-delegation on dynamically added elements 您需要在动态添加的元素上使用事件委托

$(document).on('change','.chek',function(){
    $(this).closest('.check-addon').css('background-color',this.checked?'green':'none');
});

You have to do like this : 您必须这样做:

1st Way: 第一种方式:

$(".chek").change(function () { // will fire when checkbox checked or unchecked

    if (this.checked) // check if it is checked
         $(this).closest(".check-addon").css('background-color', 'green'); //find parent span and add css
    else
    $(this).closest(".check-addon").css('background-color','transparent');

})

FIDDLE: 小提琴:

http://jsfiddle.net/2u697b57 http://jsfiddle.net/2u697b57

2nd Way: 第二种方式:

More better approach is to create a css class and add/remove it on check/uncheck: 更好的方法是创建一个css类,并在check / uncheck上添加/删除它:

CSS: CSS:

.green
{
background-color:green;
}

JQUERY: JQUERY:

$(".chek").change(function () {

    if (this.checked) 
        $(this).closest(".check-addon").addClass("green");
    else
        $(this).closest(".check-addon").removeClass("green");

FIDDLE: 小提琴:

http://jsfiddle.net/2u697b57/1/ http://jsfiddle.net/2u697b57/1/

You need to use click/change event to detect the changes on checkboxes. 您需要使用点击/更改事件来检测复选框的更改。 Use: 采用:

$('input').change(function(){
 if (this.checked) 
  $(this).parent().css('background-color','green');
 else
  $(this).parent().css('background-color','none');
});

Demo 演示版

You can use this: 您可以使用此:

$("input:checkbox").on("change", function(){
    if($(this).prop("checked")){
        $(this).parent(".check-addon").css('background-color','green');
        }
    else{
        $(this).parent(".check-addon").css('background-color','white');
    }
});

fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM