只在 JavaScript 中存在值时显示 div。

huangapple go评论100阅读模式
英文:

Show div only in case it has value in js

问题

我打算,如果单选按钮的div是“是”,则显示一开始就具有"display: none"的div。如果不是,保持该div隐藏。

英文:

I have the following code to show values ​​in a form:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var data = [
   {Queda:  &quot;Sim&quot;, Outro: &quot;Teste1&quot;, },
];

for (var i = 0; i &lt; data.length; i++) {
  var Queda = data[0].Queda;
  var Outro = data[0].Outro;
  
  if(Queda == &#39;Sim&#39;){
    $(&#39;#quedpresa&#39;).prop(&#39;checked&#39; , true);

  }else if(Queda == &#39;N&#227;o&#39;){
    $(&#39;#quedpresa1&#39;).prop(&#39;checked&#39; , true);
  }
  $(&#39;#quedpressa1&#39;).val(Outro);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;a href=&quot;#&quot; class=&quot;dropdown-item btn btn-warning upd_qued&quot;&gt;&lt;i class=&quot;metismenu-icon pe-7s-note&quot;&gt;&lt;/i&gt; Alterar&lt;/a&gt;

&lt;form method=&quot;POST&quot; class=&quot;row g-3 insquedaa2&quot;&gt;

  &lt;div class=&quot;col-xs-6 col-sm-3&quot;&gt;
    &lt;label class=&quot;form-label&quot;&gt;Queda Presenciada? &lt;/label&gt;
    &lt;input type=&quot;radio&quot; class=&quot;inradio&quot; name=&quot;quedpresa&quot; id=&quot;quedpresa&quot; value=&quot;Sim&quot;&gt;
    &lt;label for=&quot;quedpresa&quot; class=&quot;labradio&quot;&gt;Sim&lt;/label&gt;
    &lt;input type=&quot;radio&quot; class=&quot;inradio&quot; name=&quot;quedpresa&quot; id=&quot;quedpresa1&quot; value=&quot;N&#227;o&quot;&gt;
    &lt;label for=&quot;quedpresa1&quot; class=&quot;labradio&quot; style=&quot;margin-left: 2%;&quot;&gt;N&#227;o&lt;/label&gt;
  &lt;/div&gt;

  &lt;div class=&quot;col-xs-6 col-sm-3&quot; id=&quot;quedpresss&quot; style=&quot;display: none;&quot;&gt;
    &lt;label for=&quot;quedpressa1&quot; class=&quot;form-label&quot;&gt;Outro   &lt;/label&gt;
    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;quedpressa1&quot; name=&quot;quedpressa1&quot;&gt;
  &lt;/div&gt;
 &lt;/form&gt;

<!-- end snippet -->

I intend that if the div of the radio button is YES, it shows the div that has display none right at the beginning. If not keep the div hidden.

答案1

得分: 1

这是您提供的代码部分的翻译:

var data = [
   { Queda: "Sim", Outro: "Teste1" },
];

for (var i = 0; i < data.length; i++) {
  var Queda = data[0].Queda;
  var Outro = data[0].Outro;
  
  if (Queda == 'Sim') {
    $('#quedpresa').prop('checked', true);
    $('#quedpresss').show();
  } else if (Queda == 'N&#227;o') {
    $('#quedpresa1').prop('checked', true);
  }
  
  $('#quedpresa').click(() => $('#quedpresss').show());
  $('#quedpresa1').click(() => $('#quedpresss').hide());
  
  $('#quedpressa1').val(Outro);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" class="dropdown-item btn btn-warning upd_qued"><i class="metismenu-icon pe-7s-note"></i> Alterar</a>
<form method="POST" class="row g-3 insquedaa2">
  <div class="col-xs-6 col-sm-3">
    <label class="form-label">Queda Presenciada? </label>
    <input type="radio" class="inradio" name="quedpresa" id="quedpresa" value="Sim">
    <label for="quedpresa" class="labradio">Sim</label>
    <input type="radio" class="inradio" name="quedpresa" id="quedpresa1" value="N&#227;o">
    <label for="quedpresa1" class="labradio" style="margin-left: 2%;">N&#227;o</label>
  </div>
  <div class="col-xs-6 col-sm-3" id="quedpresss" style="display: none;">
    <label for="quedpressa1" class="form-label">Outro   </label>
    <input type="text" class="form-control" id="quedpressa1" name="quedpressa1">
  </div>
</form>

这些是代码部分的翻译,没有其他内容。

英文:

Well then use jQuery to show it right there where you want it to show (also, click listeners are added to toggle them):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var data = [
   {Queda:  &quot;Sim&quot;, Outro: &quot;Teste1&quot;, },
];

for (var i = 0; i &lt; data.length; i++) {
  var Queda = data[0].Queda;
  var Outro = data[0].Outro;
  
  if(Queda == &#39;Sim&#39;){
    $(&#39;#quedpresa&#39;).prop(&#39;checked&#39; , true);
    
    $(&#39;#quedpresss&#39;).show();

  }else if(Queda == &#39;N&#227;o&#39;){
    $(&#39;#quedpresa1&#39;).prop(&#39;checked&#39; , true);
  }
  
  $(&#39;#quedpresa&#39;).click(()=&gt;$(&#39;#quedpresss&#39;).show());
  $(&#39;#quedpresa1&#39;).click(()=&gt;$(&#39;#quedpresss&#39;).hide());
  
  $(&#39;#quedpressa1&#39;).val(Outro);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;a href=&quot;#&quot; class=&quot;dropdown-item btn btn-warning upd_qued&quot;&gt;&lt;i class=&quot;metismenu-icon pe-7s-note&quot;&gt;&lt;/i&gt; Alterar&lt;/a&gt;

&lt;form method=&quot;POST&quot; class=&quot;row g-3 insquedaa2&quot;&gt;

  &lt;div class=&quot;col-xs-6 col-sm-3&quot;&gt;
    &lt;label class=&quot;form-label&quot;&gt;Queda Presenciada? &lt;/label&gt;
    &lt;input type=&quot;radio&quot; class=&quot;inradio&quot; name=&quot;quedpresa&quot; id=&quot;quedpresa&quot; value=&quot;Sim&quot;&gt;
    &lt;label for=&quot;quedpresa&quot; class=&quot;labradio&quot;&gt;Sim&lt;/label&gt;
    &lt;input type=&quot;radio&quot; class=&quot;inradio&quot; name=&quot;quedpresa&quot; id=&quot;quedpresa1&quot; value=&quot;N&#227;o&quot;&gt;
    &lt;label for=&quot;quedpresa1&quot; class=&quot;labradio&quot; style=&quot;margin-left: 2%;&quot;&gt;N&#227;o&lt;/label&gt;
  &lt;/div&gt;

  &lt;div class=&quot;col-xs-6 col-sm-3&quot; id=&quot;quedpresss&quot; style=&quot;display: none;&quot;&gt;
    &lt;label for=&quot;quedpressa1&quot; class=&quot;form-label&quot;&gt;Outro   &lt;/label&gt;
    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;quedpressa1&quot; name=&quot;quedpressa1&quot;&gt;
  &lt;/div&gt;
 &lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 02:00:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定