Jquery CSS Checkbox未检测到已检查

时间:2017-08-24 11:21:48

标签: jquery css

我正在使用由http://doodlenerd.com/html-control/css-checkbox-generator

生成的一些自定义复选框



sbatch

$( ".button" ).click(function() {
  if(document.getElementById('terms_checkbox').checked) {
    alert('I am checked');
  } else {
    alert('I am not checked');
  }
 });

.button{
background:red;
width:200px
height:100px;
}

.control {
    font-family: arial;
    display: block;
    position: relative;
    padding-left: 30px;
    margin-bottom: 15px;
    padding-top: 3px;
    cursor: pointer;
    font-size: 16px;
}
    .control input {
        position: absolute;
        z-index: -1;
        opacity: 0;
    }
.control_indicator {
    position: absolute;
    top: 0px;
    left: 0;
    height: 30px;
    width: 35px;
    background: #efe8d9;
    border: 1px solid #000000;
}
.control-radio .control_indicator {
    border-radius: undefined%;
}

.control:hover input ~ .control_indicator,
.control input:focus ~ .control_indicator {
    background: #efe8d9;
}

.control input:checked ~ .control_indicator {
    background: #EFE8D9;
}
.control:hover input:not([disabled]):checked ~ .control_indicator,
.control input:checked:focus ~ .control_indicator {
    background: #efe8d9;
}
.control input:disabled ~ .control_indicator {
    background: #e6e6e6;
    opacity: 0.6;
    pointer-events: none;
}
.control_indicator:after {
    box-sizing: unset;
    content: '';
    position: absolute;
    display: none;
}
.control input:checked ~ .control_indicator:after {
    display: block;
}
.control-checkbox .control_indicator:after {
    left: 10px;
    top: 2px;
    width: 11px;
    height: 17px;
    border: solid #000000;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
}
.control-checkbox input:disabled ~ .control_indicator:after {
    border-color: #7b7b7b;
}




当我点击按钮时,它无法识别复选框已选中,我哪里出错了?

2 个答案:

答案 0 :(得分:2)

您的html <input/>缺少JavaScript所需的ID。它应该是<input type="checkbox" id='terms_checkbox'/>

尝试运行下面的代码段。

$(document).ready(function() {
  $(".button").click(function() {
    if (document.getElementById('terms_checkbox').checked) {
      alert('I am checked');
    } else {
      alert('I am not checked');
    }
  });
})
.button {
  background: red;
  width: 200px height:100px;
}

.control {
  font-family: arial;
  display: block;
  position: relative;
  padding-left: 42px;
  margin-bottom: 15px;
  padding-top: 3px;
  cursor: pointer;
  font-size: 16px;
}

.control input {
  position: absolute;
  z-index: -1;
  opacity: 0;
}

.control_indicator {
  position: absolute;
  top: 0px;
  left: 0;
  height: 30px;
  width: 35px;
  background: #efe8d9;
  border: 1px solid #000000;
}

.control-radio .control_indicator {
  border-radius: undefined%;
}

.control:hover input~.control_indicator,
.control input:focus~.control_indicator {
  background: #efe8d9;
}

.control input:checked~.control_indicator {
  background: #EFE8D9;
}

.control:hover input:not([disabled]):checked~.control_indicator,
.control input:checked:focus~.control_indicator {
  background: #efe8d9;
}

.control input:disabled~.control_indicator {
  background: #e6e6e6;
  opacity: 0.6;
  pointer-events: none;
}

.control_indicator:after {
  box-sizing: unset;
  content: '';
  position: absolute;
  display: none;
}

.control input:checked~.control_indicator:after {
  display: block;
}

.control-checkbox .control_indicator:after {
  left: 10px;
  top: 2px;
  width: 11px;
  height: 17px;
  border: solid #000000;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.control-checkbox input:disabled~.control_indicator:after {
  border-color: #7b7b7b;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div class="control-group">
  <label class="control control-checkbox">
    First checkbox
    <input type="checkbox" id='terms_checkbox'/>
    <div class="control_indicator"></div>
  </label>
</div>

<div class="button">Click Me</div>

另外,我确保用$(document).ready包装你的JQuery,以确保内容已加载到DOM:

答案 1 :(得分:0)

这里有很多东西。您没有选中复选框。没有ID terms_checkbox的元素。 如果您正在使用Jquery,那么将它用于每个选择而不是JS / Jquery它会更好。

要获取属性checked,您可以使用prop()attr()is()

&#13;
&#13;
$( ".button" ).click(function() {
  if($(".control-checkbox").find("input[type='checkbox']").prop("checked")) {
    alert('I am checked');
  } else {
    alert('I am not checked');
  }
 });
&#13;
.button{
background:red;
width:200px
height:100px;
}

.control {
    font-family: arial;
    display: block;
    position: relative;
    padding-left: 30px;
    margin-bottom: 15px;
    padding-top: 3px;
    cursor: pointer;
    font-size: 16px;
}
    .control input {
        position: absolute;
        z-index: -1;
        opacity: 0;
    }
.control_indicator {
    position: absolute;
    top: 0px;
    left: 0;
    height: 30px;
    width: 35px;
    background: #efe8d9;
    border: 1px solid #000000;
}
.control-radio .control_indicator {
    border-radius: undefined%;
}

.control:hover input ~ .control_indicator,
.control input:focus ~ .control_indicator {
    background: #efe8d9;
}

.control input:checked ~ .control_indicator {
    background: #EFE8D9;
}
.control:hover input:not([disabled]):checked ~ .control_indicator,
.control input:checked:focus ~ .control_indicator {
    background: #efe8d9;
}
.control input:disabled ~ .control_indicator {
    background: #e6e6e6;
    opacity: 0.6;
    pointer-events: none;
}
.control_indicator:after {
    box-sizing: unset;
    content: '';
    position: absolute;
    display: none;
}
.control input:checked ~ .control_indicator:after {
    display: block;
}
.control-checkbox .control_indicator:after {
    left: 10px;
    top: 2px;
    width: 11px;
    height: 17px;
    border: solid #000000;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
}
.control-checkbox input:disabled ~ .control_indicator:after {
    border-color: #7b7b7b;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="control-group">
    <label class="control control-checkbox">
        First checkbox
            <input type="checkbox" />
        <div class="control_indicator"></div>
    </label>
</div>

<div class="button">
Click Me
</div>
&#13;
&#13;
&#13;