简体   繁体   English

当输入字段为空时隐藏 DIV

[英]Hide DIV when input field is blank

I wonder if you could help me with an issue.我想知道你能不能帮我解决一个问题。

I am building a content template for an events page, that pulls data through using Advanced Custom Fields.我正在为事件页面构建一个内容模板,该模板使用高级自定义字段提取数据。

I have a field in the admin side which will be filled out when adding a new event.我在管理端有一个字段,在添加新事件时将填写该字段。 The field is called show_info with the ID #acf-editor-46 .该字段名为show_info ,ID 为#acf-editor-46

On some events however this will be left blank, but the DIV that wraps around the content on the frontend will still show on the template, the DIV has the class .show-info-wrapper .然而,在某些事件中,这将留空,但环绕前端内容的 DIV 仍将显示在模板上,DIV 具有.show-info-wrapper

I would like it so when the show_info field is blank, the DIV .show-info-wrapper does not display on the front end.我希望这样当show_info字段为空时,DIV .show-info-wrapper不会显示在前端。

I have made some progress from browsing around, you can see the code I have so far here:我在浏览方面取得了一些进展,你可以在这里看到我到目前为止的代码:

HTML (Just a quick testing set up): HTML(只是一个快速测试设置):

<textarea id="acf-editor-46" class="wp-editor-area" aria-hidden="true">1111</textarea>
<div class="show-info-wrapper">CONTENT</div>

JavaScript + jQuery: JavaScript + jQuery:

$(document).ready(function() {

    if($('#acf-editor-46').val() == '' ){$('.show-info-wrapper').hide();}  

    $('#acf-editor-46').on('change' , function() {

         if( this.value != ''){

               $('.show-info-wrapper').show(); 
          }
          else{
               $('.show-info-wrapper').hide(); 
         }
    });
  });

It works on JSFiddle ( http://jsfiddle.net/ha2nedfb/ ), however, it seems that on my WordPress site as the input and the DIV are not on the same DOM, it does not work.它适用于 JSFiddle ( http://jsfiddle.net/ha2nedfb/ ),但是,似乎在我的 WordPress 站点上,输入和 DIV 不在同一个 DOM 上,它不起作用。

Could anyone help me with this?有人可以帮我解决这个问题吗?

Thank you!谢谢!

Just change the event of #acf-editor-46 to input .只需将#acf-editor-46的事件更改为input

$(document).ready(function() {

    if($('#acf-editor-46').val() == '' ){$('.show-info-wrapper').hide();}  

    $('#acf-editor-46').on('input' , function() {  // Just change event to input

         if( this.value != ''){

               $('.show-info-wrapper').show(); 
          }
          else{
               $('.show-info-wrapper').hide(); 
         }
    });
  });
if (!$("#acf-editor-46").val()) {
// textarea is empty
}

try this to check textarea is empty or not试试这个来检查 textarea 是否为空

You can delegate event if textarea is rendered later.如果稍后呈现 textarea,您可以委托事件。

$(document).on('change', '#acf-editor-46', callback);

Links:链接:

  1. https://api.jquery.com/on/ https://api.jquery.com/on/
  2. https://learn.jquery.com/events/event-delegation/ https://learn.jquery.com/events/event-delegation/

Assuming the <textarea> and <div> are really siblings in this very order (your text contradicts the example) & if you're OK with adding a placeholder to the textarea & you only need new-ish browsers , there is a pure CSS solution:假设<textarea><div>在这个顺序中真的是兄弟姐妹(你的文本与示例相矛盾)&如果你可以在textarea添加一个placeholder并且你只需要新的浏览器,那么有一个纯 CSS解决方案:

<textarea ... placeholder=" "> </textarea>
.show-info-wrapper {
  display: block;
}

.wp-editor-area:placeholder-shown + .show-info-wrapper {
  display: none;
}

Here's a pen .这是一支笔

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

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