简体   繁体   English

切换具有特定属性复选框的元素

[英]Toggle element with checkbox with certain attribute

So I have this list of child pages sorted by title. 因此,我有按标题排序的子页面列表。 And I am using custom fields wordpress plugin. 我正在使用自定义字段wordpress插件。

This is one of the lines in content of that li element 这是该li元素的内容之一

<p>City: <?php the_field('city',$page->ID); ?></p>

I want to make checkbox (somewhere on the page) so that if you uncheck that check box, element with, lets say city=NewYork, will disappear. 我要选择一个复选框(在页面上的某个位置),以便取消选中该复选框时,带有诸如city = NewYork的元素将消失。

Code so far: 到目前为止的代码:

                        <input type="checkbox" class="newyork" name="beograd" checked>Chicago<br>
                        <input type="checkbox" class="cicago" name="Chicago" checked>Pancevo<br>

                        <?php
                    $mypages = get_pages( array( 'child_of' => $post->ID, 'meta_value' => '', 'sort_column' => 'city', 'sort_order' => 'asc' ) );

                    foreach( $mypages as $page ) {      
                        $content = $page->post_content;
                        if ( ! $content ) 

                        $content = apply_filters( 'the_content', $content );
                    ?>

                        <li class="clan">
                            <a class="noselect"><?php the_field('naziv',$page->ID); ?></a>

                            <div class="clan_content">
                                    <div class="podaci">

                                        <p>City: <?php the_field('city',$page->ID); ?></p>

                            </div>
                        </li>



                    <?php
                    }   
                ?>

SUCCESS 成功

added <li class="clan" title="<?php the_field('city',$page->ID); ?>"> 添加了<li class="clan" title="<?php the_field('city',$page->ID); ?>">

And then 接着

$(function () {
                                  $('#newyork').change(function () {            
                                     $(".clan_content").stop().slideUp();                                     
                                     $("[title=NewYork]").toggle(this.checked);
                                  }).change(); 
                                });

If you have access to jQuery something like this may work. 如果您可以访问jQuery,则可能会发生这种情况。 And it could be expanded to have the key and value set as attributes, so it isn't so hard coded. 而且可以将其扩展为将键和值设置为属性,因此它并不是那么硬编码。

 $(function(){ // Listen for a filter click $('.stateFilter').click(function(){ var ref = $(this), // The input state = ref.attr('state'), // The state I want to filter states = $('div.state[state='+state+']'); // What I should filter if (ref.is(':checked')) { // If checked, show, otherwise hide states.show(); } else { states.hide(); } }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="state" state="newYork">New York</div> <div class="state" state="newYork">Pittsford</div> <div class="state" state="newYork">Rye</div> <div class="state" state="alabama">Birmingham</div> <div class="state" state="alabama">Montgomery</div> <div class="state" state="alabama">Auburn</div> <hr/> <!-- Create Filters for Each Key you want to filter on, here I added a state attribute to the input tags, which I can reference from jQuery --> <p> <input class="stateFilter" type="checkbox" state="newYork" label="check_newYork" checked/> <label for="check_newYork">Show <b>New York</b> Cities</label> </p> <p> <input class="stateFilter" type="checkbox" state="alabama" label="check_alabama" checked/> <label for="check_alabama">Show <b>Alabama</b> Cities</label> </p> 

I took another stab with the latest info. 我又刺了一下最新信息。 I converted the php into what the html should be, but i'm not sure if it achieves everything. 我将php转换为html应该是什么,但是我不确定它是否可以实现所有功能。 And I needed to add more attributes to simplify everything. 而且我需要添加更多属性来简化所有操作。

 $(function(){ // Auto-Generate checkboxes and sort them, via jQuery magic var cities = [], cityMap = {}, i, city, clan_filters = $('#clan_filters'), p, items; // Collect cities, eliminate duplicates $('#clans li.clan').each(function(){ var ref = $(this), city = ref.attr('city'); // Make sure we don't duplicate the cities by using maps if (!cityMap[city]) { // If I haven't see this city, update the make and list cityMap[city] = true; cities.push(city); } }); // Clean out the map, not needed cityMap = undefined; // Build the checkboxes for (i = 0; i < cities.length; i++) { city = cities[i]; p = $('<p></p>').appendTo(clan_filters); p.append($('<input type="checkbox" class="clan_filter" checked/>').attr('value', city)); p.append($('<span></span>').text(city)); } // Get this reference to save time items = $('p', clan_filters); // Sort the chjeckboxes items.sort(function(a,b){ var keyA = $(a).text(); var keyB = $(b).text(); if (keyA < keyB) return -1; if (keyA > keyB) return 1; return 0; }); // Re-attached the sorted items, but this time in order $.each(items, function(i, li){ clan_filters.append($(li)); }); // Event Handlers $('input.clan_filter[type=checkbox]', clan_filters).change(function(){ var ref = $(this), // The input checked = ref.prop('checked'), // Am i checked? city = ref.val(); // What city is this for? $('#clans li.clan[city='+city+']').toggle(checked); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="clan_filters"> </div> <!-- Commenting out code since this needs to be html <ul id="clans"> <?php $mypages = get_pages( array( 'child_of' => $post->ID, 'meta_value' => '', 'sort_column' => 'city', 'sort_order' => 'asc' ) ); foreach( $mypages as $page ) { $content = $page->post_content; if ( ! $content ) $content = apply_filters( 'the_content', $content ); ?> <li class="clan" city="<?php the_field('city',$page->ID); ?>"> <a class="noselect"><?php the_field('naziv',$page->ID); ?></a> <div class="clan_content"> <div class="podaci"> <p>City: <?php the_field('city',$page->ID); ?></p> </div> </div> </li> <?php } ?> </ul> --> <!-- This is what I think the output should look like --> <ul id="clans"> <li class="clan" city="Pancevo"> <a class="noselect">What is this? B</a> <div class="clan_content"> <div class="podaci"> <p>City: Pancevo</p> </div> </div> </li> <li class="clan" city="Chicago"> <a class="noselect">What is this? A</a> <div class="clan_content"> <div class="podaci"> <p>City: Chicago</p> </div> </div> </li> <ul> 

You should be able to do that with jQuery 您应该可以使用jQuery做到这一点

$('checkbox').click(function () {
    if(!$(this).is(':checked')) {
        $('[city="New York"]').hide();
    }
});

如何切换`<textarea> <i>`'s `spellcheck` attribute with a checkbox?</div></i> <b>` 的 `spellcheck` 属性带有复选框?</div></b></textarea><div id="text_translate"><p> 我想要一个复选框,选中后将启用<textarea>的spellcheck属性,反之亦然。</p><p> 但是,我不确定这是否可能。</p><p> 到目前为止,这是我的代码: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> document.getElementById('sampleCheckbox').addEventListener('click', function() { let textArea = document.getElementById('sampleTextarea'); textArea.setAttribute('spellcheck', 'true') =.textArea,setAttribute('spellcheck'; 'false') });</pre><pre class="snippet-code-html lang-html prettyprint-override"> Enable/Disable Spellcheck <input type="checkbox" id="sampleCheckbox" checked> <br> <br> <textarea spellcheck="true" rows="5" cols="50" id="sampleTextarea">sadadadadadsasamkdajdakmkmxzcm</textarea></pre></div></div><p></p><p> 我如何实现这个目标?</p></div> - How to toggle a `<textarea>`’s `spellcheck` attribute with a checkbox?

暂无
暂无

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

相关问题 复选框上的切换元素已选中 - Toggle element on checkbox checked 通过切换将属性添加到复选框“ checked” - add attribute to checkbox “checked ” with toggle 在复选框状态上切换元素类 - Toggle element class on checkbox state 使用VueJS根据复选框值切换输入元素的禁用属性 - Toggle input element's disabled attribute depending on a checkbox value using VueJS 单击某些区域时切换复选框 - Toggle a checkbox when certain areas are clicked 如何切换`<textarea> <i>`'s `spellcheck` attribute with a checkbox?</div></i> <b>` 的 `spellcheck` 属性带有复选框?</div></b></textarea><div id="text_translate"><p> 我想要一个复选框,选中后将启用<textarea>的spellcheck属性,反之亦然。</p><p> 但是,我不确定这是否可能。</p><p> 到目前为止,这是我的代码: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> document.getElementById('sampleCheckbox').addEventListener('click', function() { let textArea = document.getElementById('sampleTextarea'); textArea.setAttribute('spellcheck', 'true') =.textArea,setAttribute('spellcheck'; 'false') });</pre><pre class="snippet-code-html lang-html prettyprint-override"> Enable/Disable Spellcheck <input type="checkbox" id="sampleCheckbox" checked> <br> <br> <textarea spellcheck="true" rows="5" cols="50" id="sampleTextarea">sadadadadadsasamkdajdakmkmxzcm</textarea></pre></div></div><p></p><p> 我如何实现这个目标?</p></div> - How to toggle a `<textarea>`’s `spellcheck` attribute with a checkbox? 使用id为&#39;checkbox&#39;+ i的复选框切换id为&#39;user&#39;+ i的属性 - Toggle a attribute with id 'user'+i with a checkbox with id 'checkbox'+i Unobtrusive JS:在表单中使用复选框切换元素可见性 - Unobtrusive JS: toggle element visibility with checkbox in a form 有没有办法使用 CSS 切换复选框输入与另一个元素? - Is there a way to toggle a checkbox input with another element with CSS? 修复切换复选框元素中的所有行为 - Fixing toggle all behavior in checkbox element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM