[英]Trying to toggle a checkbox when another element is clicked
I have p tag and when I click on the p tag I want to toggle the checkbox's check. 我有p标签,当我单击p标签时,我想切换复选框的复选框。 right now it only works on the first toggle.
现在,它仅适用于第一个切换。 when I click on the p the box gets checked and when I click on it again the box gets unchecked but that's it.
当我单击p时,该框被选中;当我再次单击它时,该框被取消选中,仅此而已。 If I click on it again nothing happens on the screen.
如果我再次单击它,屏幕上什么也不会发生。 when I inspect element in firebug the checked attribute appears and disappears, which I think is what is supposed to happen, but nothing appears on the screen.
当我检查萤火虫中的元素时,选中的属性出现和消失,我认为这是应该发生的,但屏幕上什么也没有出现。 I tried using prop() and it didn't work.
我尝试使用prop(),但没有用。 here's what I have with attr()
这就是attr()的功能
$(".clickMe").on("click", function(){
if($("#thisBox").attr("checked") ){
$("#thisBox").removeAttr("checked")
}else{
$("#thisBox").attr("checked","checked")
}
})
And could someone tell me if there is a way to check to see if an element changes. 有人可以告诉我是否有一种方法可以检查元素是否发生了变化。 Like
**if**($(el).changes(){ do this } )
what's the pattern for that 像
**if**($(el).changes(){ do this } )
这样的模式是什么
Try this 尝试这个
$(".clickMe").on("click", function(){ if($('#thisBox')[0].checked){ $('#thisBox').prop('checked', false); }else{ $('#thisBox').prop('checked', true); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <input type="checkbox" id="thisBox" /> Testing Checkbox <input type="button" class="clickMe" text="test" value="testing button"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.