简体   繁体   English

Jquery根据输入值显示或隐藏元素?

[英]Jquery show or hide elements based on input value?

Hey I would like to know how to show a div and hide another at the same time. 嘿,我想知道如何显示div并同时隐藏另一个div。 And that it only does that when I give an input of 5 or higher : 并且当我输入5或更高输入时它才会这样做

<div class="InputField InputMeters">
   <input type="tel" name="iFenceMeters" id="FenceMeters" class="AutosubmitCalculator" 
          data-minimum-length="1" tabindex="1" placeholder="00" maxlength="3" value="">
   <div class="FormExclamation Tipped Hidden" id="FormCalculatorExclamationFence">0</div>
</div>

this needs to show: <div id="testje" class="CalculatorRight" style="display:none"> 这需要显示: <div id="testje" class="CalculatorRight" style="display:none">

This needs to hide: <div id="SummaryHTML" class="CalculatorRight"> 这需要隐藏: <div id="SummaryHTML" class="CalculatorRight">

The code I use now that only focuses on input and not on input's value: 我现在使用的代码关注输入而不关注输入的值:

$('#FenceMeters').on('input', function() {
      $('#testje').show();
      $('#SummaryHTML').hide();
});

You can use the current value and check using Number.parseInt 您可以使用当前值并使用Number.parseInt进行检查

 $('#FenceMeters').on('input', function() { const value = $(this).val(); if (Number.parseInt(value) >= 5) { $('#testje').show(); $('#SummaryHTML').hide() } else { $('#testje').hide(); $('#SummaryHTML').show() } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="InputField InputMeters"> <input type="tel" name="iFenceMeters" id="FenceMeters" class="AutosubmitCalculator" data-minimum-length="1" tabindex="1" placeholder="00" maxlength="3" value=""> <div class="FormExclamation Tipped Hidden" id="FormCalculatorExclamationFence">0</div> </div> <div id="testje" class="CalculatorRight" style="display:none">hidden</div> <div id="SummaryHTML" class="CalculatorRight">shown</div> 

try below solution. 尝试以下解决方案

 $('#FenceMeters').on("change paste keyup", function() { var val = $(this).val(); if (val >= 5) { $('#testje').show(); $('#SummaryHTML').hide(); } else { $('#testje').hide(); $('#SummaryHTML').show(); } }); 
 .hidden { display: none; } .CalculatorRight { border: 1px solid red; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="InputField InputMeters"> <input type="tel" name="iFenceMeters" id="FenceMeters" class="AutosubmitCalculator" data-minimum-length="1" tabindex="1" placeholder="00" maxlength="3" value=""> <div class="FormExclamation Tipped Hidden" id="FormCalculatorExclamationFence">0</div> </div> <div id="testje" class="CalculatorRight hidden"> testje </div> <br/> <div id="SummaryHTML" class="CalculatorRight">SummaryHTML </div> 

You can use like below, 你可以使用如下,

   $('#FenceMeters').on('input', function() {
     if($('#FenceMeters').val() != "" && Number($('#FenceMeters').val()) >=5){
              $('#testje').show();
              $('#SummaryHTML').hide();
     }
   });

Get numeric value of the input by using parseFloat , if not a number in the input use 0 as default 使用parseFloat获取输入的数值,如果不是输入中的数字,则默认使用0

$('#FenceMeters').on('input', function() {
         var inputValue = parseFloat($(this).val()) || 0;
         if(inputValue>5){
                  $('#testje').show();
                  $('#SummaryHTML').hide();
         }
       });

No Jquery! 没有Jquery! Just vanilla javascript. 只是香草javascript。 Added eventListner on input keyup and changed the style for both div based on condition. 在输入keyup上添加了eventListner ,并根据条件更改了div的样式。 Just for reference. 仅供参考。

 let elem = document.getElementById("FenceMeters"); let testje = document.getElementById("testje"); let SummaryHTML = document.getElementById("SummaryHTML"); elem.addEventListener('keyup', function(){ if(elem.value > 5){ testje.style.display = null; SummaryHTML.style["display"] = "none"; } else{ testje.style["display"] = "none"; SummaryHTML.style["display"] = null; } }); 
 <div > <input type="number" id="FenceMeters" maxlength="3" > <div >0</div> </div> <div id="testje" style="display:none"> testje </div> <div id="SummaryHTML" > SummaryHTML </div> 

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

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