繁体   English   中英

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

[英]Toggle element with checkbox with certain attribute

因此,我有按标题排序的子页面列表。 我正在使用自定义字段wordpress插件。

这是该li元素的内容之一

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

我要选择一个复选框(在页面上的某个位置),以便取消选中该复选框时,带有诸如city = NewYork的元素将消失。

到目前为止的代码:

                        <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
                    }   
                ?>

成功

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

接着

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

如果您可以访问jQuery,则可能会发生这种情况。 而且可以将其扩展为将键和值设置为属性,因此它并不是那么硬编码。

 $(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> 

我又刺了一下最新信息。 我将php转换为html应该是什么,但是我不确定它是否可以实现所有功能。 而且我需要添加更多属性来简化所有操作。

 $(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> 

您应该可以使用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.

相关问题 复选框上的切换元素已选中 通过切换将属性添加到复选框“ checked” 在复选框状态上切换元素类 使用VueJS根据复选框值切换输入元素的禁用属性 单击某些区域时切换复选框 如何切换`<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> 使用id为&#39;checkbox&#39;+ i的复选框切换id为&#39;user&#39;+ i的属性 Unobtrusive JS:在表单中使用复选框切换元素可见性 有没有办法使用 CSS 切换复选框输入与另一个元素? 修复切换复选框元素中的所有行为
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM