简体   繁体   English

单击按钮时,使用 JQuery 显示带有选中复选框的 div

[英]When click on button, show divs with checked checkbox using JQuery

I want to use JQuery on my Coldfusion application for showing/hiding div elements with checkbox checked/unchecked within the div.我想在我的 Coldfusion 应用程序上使用 JQuery 来显示/隐藏 div 元素,并在 div 中选中/取消选中复选框。

Basically, in a view I show multiple divs elements, every div have also more divs inside, one of these internal divs contains an input type checkbox that could come checked or unchecked.基本上,在我显示多个 div 元素的视图中,每个 div 内部还有更多 div,其中一个内部 div 包含一个输入类型复选框,可以选中或取消选中。

I also have three buttons in that view 'Active, Inactive, All'.我在该视图中也有三个按钮“活动、非活动、全部”。 When clicking on Active I want to show all div elements with checkbox checked, not showing the unchecked, and the other way around when clicking on Inactive .单击 Active 时,我想显示所有选中复选框的 div 元素,而不显示未选中的元素,反之,单击 Inactive 时

<div class="btn-group ">
  <button id="actives" type="button">Actives</button>
  <button id="inactives" type="button">Inactives</button>
  <button id="all" type="button">All</button>
</div>

<div id="apiDiv">
  <cfloop array="#apis#" index="api">
    <div class="card card-found">
      <div class="card-header">
        <cfif Len(api.iconClass)>
          <i class="fa fa-fw #api.iconClass#"></i>
        </cfif>
        #structKeyExists( api, "name" ) ? api.name : api.id#
      </div>
      <div class="card-body">
        <p>#api.description#</p>
      </div>
      <div class="card-button">
        <input class="#inputClass# ace ace-switch ace-switch-3" name="#inputName#" id="#inputId#-#api.id#" type="checkbox"  value="#HtmlEditFormat( api.id )#"<cfif ListFindNoCase( value, api.id )> checked="checked"</cfif> tabindex="#getNextTabIndex()#">
        <span class="lbl"></span>
      </div>
    </div>
  </cfloop>
</div>

I´m not an expert at all with JQuery.我根本不是 JQuery 的专家。 The only thing I have done is what follows and I do not know whether if is a good beggining or not:我唯一做的就是下面的事情,我不知道这是否是一个好的开始:

$("#actives").click(function (e) {
  $("#apiDiv .card").filter(function() {
    <!--- code here --->
  });
});

Someone please that can help me with it?请问有人可以帮我吗? Thanks a lot in advance!提前非常感谢!

After your CF code executes, it will generate a .card for each loop iteration of your apis array.在您的 CF 代码执行后,它将为您的apis数组的每个循环迭代生成一个.card So you jQuery code will need a click handler for the #actives button and that will loop through each() iteration of the checkboxes to determine the checked/unchecked state.因此,您的 jQuery 代码将需要#actives按钮的单击处理程序,它将循环遍历复选框的each()迭代以确定选中/未选中的 state。 At that point find the closest() ancestor .card and show() / hide() the .card depending upon the checkbox state.此时,根据复选框 state 找到closest()祖先.cardshow() / hide() .card

$("#actives").click(function (e) {
    $('input[type=checkbox]').each(function() {
        if (this.checked) {
            $(this).closest(".card").show();
        } else {
            $(this).closest(".card").hide();
        }
    });
});

Call the checkbox by its id and when it's checked, write a function to display the divs you want to display:通过它的 id 调用复选框,当它被选中时,写一个 function 来显示你想要显示的 div:

<input type="checkbox" id="check">
$document.getElementById("check").onclick = function(){
    $document.getElementById("div_name").style.display="block";   // block displays the div.

}

If you want to do it with jQuery code:如果您想使用 jQuery 代码执行此操作:

$('#actives').click(function(){
        $('#apiDiv').show();
  });

Working Fiddle工作小提琴

The code you are probably looking for is in these event handlers for your buttons:您可能正在寻找的代码在您的按钮的这些事件处理程序中:

function activesHandler() {
  jQuery(".card-button > input:checked").parents(".card.card-found").show();
  jQuery(".card-button > input:not(:checked)").parents(".card.card-found").hide();
}

function inactivesHandler() {
  jQuery(".card-button > input:checked").parents(".card.card-found").hide();
  jQuery(".card-button > input:not(:checked)").parents(".card.card-found").show();
}

function allHandler() {
  jQuery(".card.card-found").show();
}

jQuery("#actives").click(activesHandler);
jQuery("#inactives").click(inactivesHandler);
jQuery("#all").click(allHandler);

I reproduced some of your ColdFusion by replacing it with JavaScript and provided a demonstration of the above event handlers in this JSFiddle .我通过用 JavaScript 替换它来复制您的一些 ColdFusion,并在此JSFiddle中提供了上述事件处理程序的演示。

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

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