簡體   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