当输入字段为空时显示/隐藏div(jquery,javascript)

jhu*_*lio 0 javascript jquery

我有一个div内容,默认隐藏,我想在用户输入时在输入字段中显示它#control

<form>
<input name="control" value="" id="control" />
<div class="show_hide">
  //some content here........
</div>
</form>
Run Code Online (Sandbox Code Playgroud)

gur*_*dio 5

// Bind keyup event on the input
$('#control').keyup(function() {
  
  // If value is not empty
  if ($(this).val().length == 0) {
    // Hide the element
    $('.show_hide').hide();
  } else {
    // Otherwise show it
    $('.show_hide').show();
  }
}).keyup(); // Trigger the keyup event, thus running the handler on page load
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
  <input name="control" id="control" />
  <div class="show_hide">
    //some content here........
  </div>
</form>
Run Code Online (Sandbox Code Playgroud)

在keyup上检查输入的值,如果length为0表示为空,则隐藏div,否则显示