简体   繁体   English

如何通过单击单选按钮来切换 div?

[英]How to toggle div by clicking radio button?

By default the both reagent_code should be hidden.默认情况下,应该隐藏这两个reagent_code By selecting the radio button, have to show the appropriate DIV (following its #id).通过选择单选按钮,必须显示适当的 DIV(在其 #id 之后)。

 function marketer(x) { if (x == 0) { document.getElementById('reagent_code').style.display = 'none'; } else if (x == 1) { document.getElementById('reagent_code').style.display = 'block'; return; } }
 <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="marketer" id="yes" onclick="marketer(1)" value="1"> <label class="form-check-label" for="yes">Yes</label> </div> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="marketer" id="no" onclick="marketer(0)" value="0"> <label class="form-check-label" for="no">No</label> </div>

I get this error.我收到这个错误。

TypeError: marketer is not a function类型错误:营销人员不是函数

Following suggestion of AvcS to your code, without any other library.遵循AvcS对您的代码的建议,没有任何其他库。

The function marketer should be defined on global scope, use window.marketer = function, if you are defining it inside a function函数marketer应该在全局范围内定义,使用window.marketer = function,如果你在函数中定义它

 window.marketer = function(x) { if (x == 0) { document.getElementById('reagent_code').style.display = 'none'; } else if (x == 1) { document.getElementById('reagent_code').style.display = 'block'; return; } }
 <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="marketer" id="yes" onclick="marketer(1)" value="1"> <label class="form-check-label" for="yes">Yes</label> </div> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="marketer" id="no" onclick="marketer(0)" value="0"> <label class="form-check-label" for="no">No</label> </div> </div> <div id='reagent_code'> My amazing div </div>

However, as suggested by Rory McCrossan , you should review your code.但是,正如Rory McCrossan所建议的,您应该检查您的代码。

Try This尝试这个

<div class="form-check form-check-inline">
    <input class="form-check-input" type="radio" name="marketer" id="yes" onclick="marketer(1)" value="1">
    <label class="form-check-label" for="yes">Yes</label>
  </div>
  <div class="form-check form-check-inline">
    <input class="form-check-input" type="radio" name="marketer" id="no" onclick="marketer(0)" value="0">
    <label class="form-check-label" for="no">No</label>
  </div>
  <!-- div to hide and show -->
  <div id="div1" style="display: none;">
      <h1>this is div 1 will show if yes</h1>
  </div>
  <div id="div2" style="display: none;">
      <h1>this is div 2 will show if no</h1>
  </div>
<script>
    function marketer(x) {
          if (x == 0) {
            document.getElementById('div1').style.display = 'none';
            document.getElementById('div2').style.display = 'block';
          } else if (x == 1) {
            document.getElementById('div1').style.display = 'block';
            document.getElementById('div2').style.display = 'none';
            return;
          }
        }
</script>
  1. make sure you do not use the same function names and elements names确保不要使用相同的函数名称和元素名称
  2. use an event listener and toggle the element使用事件侦听器并切换元素

jQuery jQuery

 $("[name=marketer]").on("click", function() { console.log(this.value) $('#reagent_code').toggle(this.value == 1) }) $("[name=marketer]:checked").click()
 #Reagent { display: none }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="marketer" checked id="yes" value="1"> <label class="form-check-label" for="yes">Yes</label> </div> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="marketer" id="no" value="0"> <label class="form-check-label" for="no">No</label> </div> <div id="reagent_code">Reagent</div>

Plain JS纯JS

 [...document.querySelectorAll("[name=marketer]")].forEach(function(elem) { elem.addEventListener("click", function() { console.log(this.value) document.getElementById('reagent_code').style.display = this.value === "0" ? 'none' : 'block'; }) if (elem.checked) { elem.click(); } })
 #Reagent { display:none }
 <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="marketer" checked id="yes" value="1"> <label class="form-check-label" for="yes">Yes</label> </div> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="marketer" id="no" value="0"> <label class="form-check-label" for="no">No</label> </div> <div id="reagent_code">Reagent</div>

Try this simple code below :试试下面这个简单的代码:

 $('input[type=radio]').change(function(){ if($(this).val() == 'ON') { return $('.content').css('display', 'block'); } return $('.content').css('display', 'none'); });
 <input type="radio" name="radio" id="radio-1" value="ON" checked=""> <label for="radio-1">ON</label> <input type="radio" name="radio" id="radio-2" value="OFF"> <label for="radio-2">OFF</label> <div class="content"> Hello world. </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

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