admin管理员组文章数量:1332404
Hi Please check below code -
HTML
<div class="manage-user-freind" style="line-height:18px; cursor:pointer;">
<span style="color:blue;">
<img src="'+pageUrl+'/img/user-16x16.png" height="14" style="float:left; padding-top:2px;" /><input class="chk" type="checkbox" value="10" />pieter
</span>
</div>
<div class="manage-user-freind" style="line-height:18px; cursor:pointer;">
<span style="color:blue;">
<img src="'+pageUrl+'/img/user-16x16.png" height="14" style="float:left; padding-top:2px;" /><input class="chk" type="checkbox" value="12" />john
</span>
</div>
JQuery
$('.manage-user-freind').click(function(){
$(this).toggleClass('clicked',function(){
$('.chk').prop('checked', true);
});
});
CSS
.clicked {
background-color: yellow;
}
I have used toggleClass() function. toggleClass
is working fine but all the check-boxes clicked also not unchecked the current checkbox .
I need when I will click any div first show that div in yellow color along with current checkbox checked. Again clicking on that yellow color div will remove and also checkbox will unchecked.
Thanks.
Sorry for my English wording. :(
Hi Please check below code -
HTML
<div class="manage-user-freind" style="line-height:18px; cursor:pointer;">
<span style="color:blue;">
<img src="'+pageUrl+'/img/user-16x16.png" height="14" style="float:left; padding-top:2px;" /><input class="chk" type="checkbox" value="10" />pieter
</span>
</div>
<div class="manage-user-freind" style="line-height:18px; cursor:pointer;">
<span style="color:blue;">
<img src="'+pageUrl+'/img/user-16x16.png" height="14" style="float:left; padding-top:2px;" /><input class="chk" type="checkbox" value="12" />john
</span>
</div>
JQuery
$('.manage-user-freind').click(function(){
$(this).toggleClass('clicked',function(){
$('.chk').prop('checked', true);
});
});
CSS
.clicked {
background-color: yellow;
}
I have used toggleClass() function. toggleClass
is working fine but all the check-boxes clicked also not unchecked the current checkbox .
I need when I will click any div first show that div in yellow color along with current checkbox checked. Again clicking on that yellow color div will remove and also checkbox will unchecked.
Thanks.
Sorry for my English wording. :(
Share Improve this question asked May 20, 2015 at 7:28 DeveloperDeveloper 2,7068 gold badges45 silver badges65 bronze badges4 Answers
Reset to default 5Try this:
$('.manage-user-freind').on('click', function() {
$(this).toggleClass('clicked').find('.chk').prop('checked', $(this).hasClass('clicked'));
});
Demo: https://jsfiddle/tusharj/d84z5vub/1/
$.toggleClass
don't have callback function. It's not async function, so you can move $('.chk').prop('checked', true);
to next statement along toggling class:
$(this).toggleClass('clicked');
$('.chk').prop('checked', true);
To check and uncheck, this code should work:
$('.manage-user-freind').click(function(){
$(this).toggleClass('clicked');
var checkbox = $(this).find('.chk');
checkbox.prop('checked', !checkbox.prop("checked"));
});
Here is a Fiddle.
According to this: jquery function on toggleClass plete?
you can use premise to wait until class will be changed
$(this).toggleClass('clicked').promise().done(function(){
alert('a');
});
本文标签: javascriptJQuery toggleClass() checkbox uncheckedStack Overflow
版权声明:本文标题:javascript - JQuery toggleClass() checkbox unchecked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742226397a2436396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论