当我选中一个复选框时,我希望它变成<p>
#0099ff
。
当我取消勾选此复选框时,我希望它恢复原状。
到目前为止,我拥有的代码:
$('#checkbox').click(function(){
if ($('#checkbox').attr('checked')) {
/* NOT SURE WHAT TO DO HERE */
}
})
当我选中一个复选框时,我希望它变成<p>
#0099ff
。
当我取消勾选此复选框时,我希望它恢复原状。
到目前为止,我拥有的代码:
$('#checkbox').click(function(){
if ($('#checkbox').attr('checked')) {
/* NOT SURE WHAT TO DO HERE */
}
})
.change()
和this.checked
:
$('#checkbox').change(function(){
var c = this.checked ? '#f00' : '#09f';
$('p').css('color', c);
});
--
关于使用 this.checked
Andy E写了一篇非常好的文章,讲述了我们过度使用jQuery的问题: 利用jQuery强大的功能来访问元素的属性。这篇文章专门讨论了使用.attr("id")
的情况,但是如果#checkbox
是一个<input type="checkbox" />
元素,那么使用$(...).attr('checked')
(甚至是$(...).is(':checked')
)与this.checked
存在相同的问题。
试试这个。
$('#checkbox').click(function(){
if (this.checked) {
$('p').css('color', '#0099ff')
}
})
有时我们会过度使用 jQuery。许多事情可以通过纯 JavaScript 用 jQuery 实现。可能会出现“this.checked”始终为“on”的情况。因此,我建议:
$('#checkbox').change(function() {
if ($(this).is(':checked')) {
console.log('Checked');
} else {
console.log('Unchecked');
}
});
$(':checkbox')
以使其工作 ;) - Pathros$('#checkbox').click(function(){
var chk = $(this);
$('p').toggleClass('selected', chk.attr('checked'));
})
#checkbox
确实是一个复选框(否则,ID名称有些误导),没有理由执行 $('#checkbox').attr('checked')
。使用this.checked
即可。 - jensgram$(document).on("click","#check",function(){
if(typeof(check_holder)=="undefined"){
//this means that it is the first time and the check is going to be checked
//do something
check_holder=1; //indicates that the is checked,it is in checked state
}
else if(check_holder==1){
//do something when the check is going to be unchecked
check_holder=0; //it means that it is not checked,it is in unchecked state
}
else if(check_holder==0){
//do something when the check is going to be checked
check_holder=1;//indicates that it is in a checked state
}
});
以上代码可用于许多情况下,以查找复选框是否已选中或未选中。其背后的概念是将复选框状态保存在变量中,即开启或关闭时。希望这个逻辑能够解决您的问题。
<!-- script to check whether checkbox checked or not using prop function -->
<script>
$('#change_password').click(function(){
if($(this).prop("checked") == true){ //can also use $(this).prop("checked") which will return a boolean.
alert("checked");
}
else if($(this).prop("checked") == false){
alert("Checkbox is unchecked.");
}
});
</script>
如果复选框被选中或取消选中,您可以使用此代码执行操作。
$('#chk').on('click',function(){
if(this.checked==true){
alert('yes');
}else{
alert('no');
}
});
$('#checkbox').on("change", function (e){
if(this.checked){
// Do one thing
}
else{
// Do some other thing
}
});
请参阅:https://www.w3schools.com/jsref/prop_checkbox_checked.asp
$('#checkbox').change(function(){
(this.checked)?$('p').css('color','#0099ff'):$('p').css('color','another_color');
});
为什么不使用内置事件?
$('#checkbox').click(function(e){
if(e.target.checked) {
// code to run if checked
console.log('Checked');
} else {
//code to run if unchecked
console.log('Unchecked');
}
});
.click()
的函数在单击事件上被调用。因此,我不明白您所说的“启用”和“禁用”的含义。如果复选框被选中,则可以调用函数a()
。但是,您必须编写反向函数以在未选中复选框时调用。我感到困惑。 - jensgram.bind()
和.unbind()
将事件绑定到另一个元素。这是你想要的吗? - jensgramprop()
来代替这个问题想要使用的attr()
方法;当时还没有创建prop()
方法。然而,关于this.checked
的答案可能仍然有用。 - trysis