隐藏按钮并显示JavaScript

时间:2018-01-15 00:13:08

标签: javascript jquery

我的javascript遇到了问题。我有两个按钮。当某个输入有值时,我们隐藏按钮,否则,我们会显示它。

以下是我们要隐藏和显示的按钮:

<button class="btn btn-info" style="visibility: hidden;" type="button" id="button1" data-id="{{receiptno}}" data-toggle="modal" data-target="#myModal" contenteditable="false">Pay</button>
<form action="/payments/report/{{receiptno}}.pdf" method=post>
  <input type=hidden value="{{receiptno}}" name="row_print"></input>
  <button class="btn btn-danger" style="visibility: hidden;" type="submit" id="anotherbutton1" name="delete">
      <span class="glyphicon glyphicon-print"></span> Print Receipt
  </button>      
</form>

我们在这里获得了价值。如果它&#39;&#39;&#39;我们会显示付款按钮,否则我们会显示打印收据按钮:

<input style="visibility: hidden;" id="referto" value="{{paymentmethod}}">

这是我们的javascript:

<script type="text/javascript">
  $(document).ready(function() {
    console.log($("#referto").val());
    if ( $("#referto").val() == '') { 
      document.getElementById('button1').style.visibility = 'visible';
    }
    else {
      document.getElementById('anotherbutton1').style.visibility = 'visible';
    }   
  });    
</script>

我真的不确定为什么这不起作用。任何帮助表示赞赏。谢谢!

2 个答案:

答案 0 :(得分:0)

你可以试试看看它是否有效吗?

if ( $("#referto").val()) { 

它涵盖了很多案例,如空字符串,空值等。

答案 1 :(得分:0)

看起来很好。我测试过如下。也许你的价值=&#34; {{paymentmethod}}&#34;并没有真正回归,而是一些空白。检查从该值返回的内容。祝你好运。

&#13;
&#13;
$(document).ready(function() {
 
  console.log($("#referto").val());
  
   if ($("#referto").val() == '' || $("#referto").val() == ' ') {        
      document.getElementById('button1').style.visibility = 'visible';
    } else {        
      document.getElementById('anotherbutton1').style.visibility = 'visible';
    }
  
  $("#referto").on("change keydown keyup", function(){
  
    document.getElementById('anotherbutton1').style.visibility = 'hidden';
    document.getElementById('button1').style.visibility = 'hidden';

      if ($("#referto").val() == '' || $("#referto").val() == ' ') {        
        document.getElementById('button1').style.visibility = 'visible';
      } else {        
        document.getElementById('anotherbutton1').style.visibility = 'visible';
      }
    
  });
  
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I have run into a problem with our javascript, let's say I have two buttons here I when a certain input has a value we hide the button, otherwise show it Here's our buttons that we want to hide and show:

<button class="btn btn-info" style="visibility: hidden;" type="button" id="button1" data-id="{{receiptno}}" data-toggle="modal" data-target="#myModal" contenteditable="false">Pay</button>


<form action="" method=post>
  <input type=hidden value="" name="row_print">

  <button class="btn btn-danger" style="visibility: hidden;" type="submit" id="anotherbutton1" name="delete"><span class="glyphicon glyphicon-print"></span> Print Receipt</button>

</form>
We are getting our values here, if its '' we show the pay button if it's not '' we show the print receipt button:

<input style="visibility: block;" id="referto" value="">
&#13;
&#13;
&#13;