简体   繁体   English

如果另一个类存在,则有条件地应用css类jquery / rails

[英]apply a css class conditionally if another class exists jquery/rails

I have a form with checkboxes - if one of these is checked, there is some jquery that applies the css class checkboxed . 我有一个带有复选框的表单-如果选中其中一个,则有一些jquery应用CSS类checkboxed

I have another div further down the page that is hidden by default - but if any of the form checkboxes are checked, it should become visible. 我在默认情况下隐藏的页面的下方还有另一个div但是,如果选中了任何形式的复选框,则它应该变得可见。

I'd like to use something like this 我想用这样的东西

<div class="<%= "hide" unless checkboxed.exists? %>">

but obviously that doesn't work. 但显然那是行不通的。 Is there a way to test if a css class exists on a given page? 有没有一种方法可以测试给定页面上是否存在CSS类? I know I'll have to use jQuery, but how to then tie this in with the conditional CSS class in the hidden div ? 我知道我将不得不使用jQuery,但是如何将其与隐藏div的条件CSS类绑定在一起?

You need to add the div showing logic to the place where you react to checking a box.The addition might look like this: 您需要将显示逻辑的div添加到您对复选框做出反应的地方。添加可能如下所示:

if($('.checkboxed').length)
    $('#hiddenDiv').show();

Your hidden div must have an ID, here I assumed id="hiddenDiv" . 您的隐藏div必须具有一个ID,在这里我假定为id="hiddenDiv"

var checkbox = $('checkbox').attr("checked");

if (checkbox == 'checked') {
    $('#divID').show(); // #divID is the ID for the hidden DIV which you want to show.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM