提问者:小点点

将文本格式添加到文本关闭复选框


我的wordpress网站上有个小问题。 我想在文本“西班牙”附近的复选框被选中时添加一个font-weight。 我在jquery中尝试了不同的代码,其中一个操作系统使用WrapInner(),但都没有成功

<div class="checkbox">
    <label for="pais_produtores-spain">
        <input type="checkbox" id="pais_produtores-spain" class="js-wpv-filter-trigger" name="wpv-pais_produtores[]" value="spain" checked="checked">
        Spain
    </label>
</div>

这个小问题最好的解决办法是什么?


共1个答案

匿名用户

使用jquery.is(':checked'),您可以查看复选框是否被选中,然后使用jquery.css()函数应用样式

null

$( "#pais_produtores-spain" ).on( "click", function(){
  if($(this).is(':checked')){
    console.log('now its checked');
    $(this).parent().css('font-weight', 'bolder');
  }else{
    console.log('not checked anymore');
    $(this).parent().css('font-weight', 'normal');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="checkbox">
    <label for="pais_produtores-spain">
        <input type="checkbox" id="pais_produtores-spain" class="js-wpv-filter-trigger" name="wpv-pais_produtores[]" value="spain" checked="checked">
        Spain
    </label>
</div>