簡體   English   中英

輸入有值時如何顯示div?

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

我有一個輸入文本和一個隱藏div。

當輸入文本具有值時,我想顯示div。

這是演示: 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>

請有人可以幫我嗎?

您可以在文本框的keyup事件中編寫代碼,以獲取在其中輸入的文本的長度。

$('input[name=amount]').keyup(function(){
  if($(this).val().length)
    $('#yeah').show();
  else
    $('#yeah').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', '');
});

這是純JavaScript和DOM,沒有jQuery

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

單擊此處獲取jsfiddle

工作提琴

使用以下邏輯:如果修剪后有一些值,則顯示其他隱藏。

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