【问题标题】:Get toggle checkbox status is it true or Not获取切换复选框状态是真还是假
【发布时间】:2017-11-29 12:29:25
【问题描述】:

在此,我没有得到复选框的状态是真还是假我尽力了我没有得到任何解决方案。当点击切换时,它总是给我一个错误的值

$('input').on("change", function() {
  if ($('input[type="checkbox"].is(":checked")')) {
    alert("its checked");
  }
});
input[type=checkbox] {
  width: 0;
  height: 0;
  visibility: hidden;
}

label {
  cursor: pointer;
  text-indent: -9999px;
  width: 50px;
  height: 20px;
  background: grey;
  display: block;
  border-radius: 100px;
  position: relative;
}

label:after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 14px;
  height: 14px;
  background: #fff;
  border-radius: 90px;
  transition: 0.3s;
}

input:checked+label {
  background: #bada55;
}

input:checked+label:after {
  left: calc(100% - 5px);
  transform: translateX(-100%);
}

label:active:after {
  width: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="toggle" id="switch">
<label for="switch">Toggle</label>

【问题讨论】:

  • 我做了一个sn-p,立即可以看到这个问题是由无法再复制的问题或简单的印刷错误引起的。虽然类似的问题可能是这里的主题,但这个问题的解决方式不太可能帮助未来的读者。这通常可以通过在发布之前识别并仔细检查重现问题所需的最短程序来避免 - 请下次在询问之前在控制台中创建一个 sn-p 外观
  • 我觉得你需要if ($(this).is(":checked") ) {

标签: jquery checkbox toggle status


【解决方案1】:

问题出在您的选择器中。您需要更改代码中' 的位置..!

$('input').on("change", function() {
    if ($('input[type="checkbox"]').is(":checked")) { //or you can use $(this) instead of $(input[type="checkbox"]) to select itself
    alert("its checked");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="toggle" id="switch">
<label for="switch">Toggle</label>

【讨论】:

    【解决方案2】:

    使用$(this).is(":checked") 代替。

    $('input').on("change", function () {
        if ($(this).is(":checked")) {
            alert("its checked");
        }
    });
    input[type=checkbox] {
        width: 0;
        height: 0;
        visibility: hidden;
    }
    
    label {
        cursor: pointer;
        text-indent: -9999px;
        width: 50px;
        height: 20px;
        background: grey;
        display: block;
        border-radius: 100px;
        position: relative;
    }
    
    label:after {
        content: '';
        position: absolute;
        top: 3px;
        left: 3px;
        width: 14px;
        height: 14px;
        background: #fff;
        border-radius: 90px;
        transition: 0.3s;
    }
    
    input:checked + label {
        background: #bada55;
    }
    
    input:checked + label:after {
        left: calc(100% - 5px);
        transform: translateX(-100%);
    }
    
    label:active:after {
        width: 30px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" name="toggle" id="switch">
       <label for="switch">Toggle</label>

    还有一点,您不需要将widthheight 设置为零,只需使用visibility

    input[type=checkbox] {
        /* width: 0; */
        /* height: 0; */
        visibility: hidden;
    }
    

    【讨论】:

    • 非常感谢。它的隐藏问题很小,但非常令人沮丧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多