简体   繁体   English

输入有值时如何显示div?

[英]How can show a div when input has a value?

I have an input text that and a hide div. 我有一个输入文本和一个隐藏div。

I want to show the div when the input text has a value. 当输入文本具有值时,我想显示div。

Here is the demo: http://jsfiddle.net/vyc7N/181/ 这是演示: http : //jsfiddle.net/vyc7N/181/

<label for="db">Type whatever</label>
<input type="text"name="amount"  />

<div id="yeah" style="display:none;">
 <input type="submit"   />
</div>

Please somebody can help me? 请有人可以帮我吗?

You can write the code in keyup event of textbox to get the length of text entered in it. 您可以在文本框的keyup事件中编写代码,以获取在其中输入的文本的长度。

$('input[name=amount]').keyup(function(){
  if($(this).val().length)
    $('#yeah').show();
  else
    $('#yeah').hide();
});

Working Demo 工作演示

you can also use .toggle() instead of using .show / .hide() : 您还可以使用.toggle()而不是.show / .show .hide()

  $('input[name=amount]').keyup(function(){
     $('#yeah').toggle($(this).val().length);
  });
$('input[name=amount]').bind('keyup change', function(){
if(!$(this).val())
    $("#yeah").css('display', 'none');
else
    $("#yeah").css('display', '');
});

This is pure javascript and DOM, no jQuery 这是纯JavaScript和DOM,没有jQuery

You could use onkeyup() : 您可以使用onkeyup()

<label>Type whatever</label>
<input id='inptOne' type="text" name="amount" onkeyup="myFunction()" />

<div id="yeah" style="display:none">
    <input type="submit" />
</div>

<script>
//if input has some value will show the button
  window.myFunction = function() {
  //alert('it ran!');
  var hasValue = document.getElementById('inptOne').value;
  if (!!hasValue) {
    document.getElementById('yeah').style.display = 'inline';
  } else {
    document.getElementById('yeah').style.display = 'none';
  };
 };
</script>

Click Here for jsfiddle 单击此处获取jsfiddle

Working fiddle 工作提琴

Use following logic : If after trimming there is some value , then show else hide. 使用以下逻辑:如果修剪后有一些值,则显示其他隐藏。

$('input[name=amount]').keyup(function(){
  if($.trim($(this).val())!='')
    $('#yeah').show();
  else
    $('#yeah').hide();
});

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

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