javascript - 为什么在 jquery 中复选框没有取消选中或者单击事件没有触发?

标签 javascript jquery

你能告诉我为什么复选框在jquery或click事件中没有被取消选中,而不是触发吗?

我的演示中有一个复选框。用户可以取消选中此复选框,但我无法取消选中此复选框。这是我的代码

https://jsbin.com/lohaleleba/edit?html,css,js,output

$(function() {
  function signUpAgreeCheckedEventHandler(e) {
    e.preventDefault();
    e.stopPropagation();
    alert('--')
  }
  $('#signUpAgree_js').click(signUpAgreeCheckedEventHandler)
})
#toi-login .form ul li {
  list-style: none;
  margin-bottom: 22px;
}

#toi-login .checkbox {
  display: block;
  margin: 0 0 12px;
}

#toi-login p {
  font-size: 14px;
  line-height: 18px;
  color: #222;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#toi-login .checkbox input[type="checkbox"] {
  display: none;
}

#toi-login input {
  display: block;
  width: 100%;
  line-height: normal;
  border: 0;
  font-size: 14px;
}

#toi-login .checkbox label:before {
  content: "";
  position: absolute;
  left: 0px;
  top: 2px;
  height: 14px;
  width: 14px;
  line-height: 15px;
  background: #ffffff no-repeat;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -ms-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
  border: 1px solid #b1b1b1;
  border-radius: 2px;
}

#toi-login .checkbox input[type="checkbox"]:checked+label:before {
  background: #3696de;
  border-color: #3696de;
}

#toi-login .checkbox input[type="checkbox"]:checked+label:after {
  position: absolute;
  content: '';
  border: solid #fff;
  width: 5px;
  height: 10px;
  left: 4px;
  top: 3px;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="toi-login">
  <form class="form" action="#" autocomplete="off">
    <ul>
      <li class="checkbox">
        <p> <input type="checkbox" checked="true" id="signUpAgree_js"> <label for="agree">I agree with the                <a href="#"> Terms &amp; Conditions </a>and                <a href="#"> Privacy Policy</a> of Times of India</label> </p>
      </li>
    </ul>
  </form>
</div>

您能告诉我如何取消选中或切换此字段吗

最佳答案

只需更正标签 for 属性即可。 for 属性确定它与哪个input 关联,在本例中,需要提供 signUpAgree_js 的 ID。

$(function() {
  function signUpAgreeCheckedEventHandler(e) {
    
    alert('--')
  }
  $('#signUpAgree_js').change(signUpAgreeCheckedEventHandler)
})
#toi-login .form ul li {
  list-style: none;
  margin-bottom: 22px;
}

#toi-login .checkbox {
  display: block;
  margin: 0 0 12px;
}

#toi-login p {
  font-size: 14px;
  line-height: 18px;
  color: #222;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#toi-login .checkbox input[type="checkbox"] {
  display: none;
}

#toi-login input {
  display: block;
  width: 100%;
  line-height: normal;
  border: 0;
  font-size: 14px;
}

#toi-login .checkbox label:before {
  content: "";
  position: absolute;
  left: 0px;
  top: 2px;
  height: 14px;
  width: 14px;
  line-height: 15px;
  background: #ffffff no-repeat;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -ms-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
  border: 1px solid #b1b1b1;
  border-radius: 2px;
}

#toi-login .checkbox input[type="checkbox"]:checked+label:before {
  background: #3696de;
  border-color: #3696de;
}

#toi-login .checkbox input[type="checkbox"]:checked+label:after {
  position: absolute;
  content: '';
  border: solid #fff;
  width: 5px;
  height: 10px;
  left: 4px;
  top: 3px;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="toi-login">
  <form class="form" action="#" autocomplete="off">
    <ul>
      <li class="checkbox">
        <p> <input type="checkbox" checked id="signUpAgree_js"> <label for="signUpAgree_js">I agree with the                <a href="#"> Terms &amp; Conditions </a>and                <a href="#"> Privacy Policy</a> of Times of India</label> </p>
      </li>
    </ul>
  </form>
</div>

关于javascript - 为什么在 jquery 中复选框没有取消选中或者单击事件没有触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48848852/

相关文章:

javascript - Angular 将 URL 中的 "/"替换为 "%2F"

JavaScript - 跨源请求被阻止 - 非服务器解决方案

javascript - Jquery UI 对话框不会消失

javascript - 从 Firebase 数据库中的两个 Promise 获取结果

javascript - 引用错误: __dirname is not defined in ES module scope building script

javascript - 在 javascript 中创建一个 util 类

javascript - 点击按钮,显示css类名

javascript - 在 JQuery 中,为什么 $(this) == $(this) 返回 false?

javascript - 扩展 Aloha Editor 的格式插件

javascript - 防止或处理同一部分中子标签和父标签的点击事件