【问题标题】:Jquery hide div based on input check valueJquery根据输入检查值隐藏div
【发布时间】:2020-01-25 17:41:27
【问题描述】:

我正在尝试根据输入检查值隐藏 div。如果该项目已被检查,它也应该工作,到目前为止我的代码不起作用。正如我想隐藏的 div 总是显示的那样?我创建了一个JS Fiddle 来复制问题

代码如下:

var test = $("input[value='home']");

if (test == true) {
  $(".title").hide();
} else {
  $(".title").show();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div role="radiogroup">
  <input type="radio" name="gender" value="home" checked> Male
  <br>
  <input type="radio" name="gender" value="female"> Female
  <br>
  <input type="radio" name="gender" value="other"> Other
  <br />
  <br />
  <div class='title'>
    This is the title not on home value
  </div>
</div>

任何输入都会很棒。

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

您只能使用 CSS 来实现:

input[name="gender"][value="home"]:checked ~ div.title {
  display: none;
}
<input type="radio" name="gender" value="home" checked> Male
<br>
<input type="radio" name="gender" value="female"> Female
<br>
<input type="radio" name="gender" value="other"> Other
<br />
<br />
<div class='title'>
    This is the title not on home value
</div>

【讨论】:

    【解决方案2】:

    你需要为radio组添加一个事件监听,然后获取checked选项的值。此外,您可能希望在页面加载时运行一次,这就是最后一行的 .change() 所做的。

    //run this anytime a radio input changes
    $('input[name="gender"]').on('change', function() {
        //get the currently selected value
        let test = $('input[name="gender"]:checked').val();
    
        //check the current value
        if (test == 'home') {
            $(".title").hide();
        } else {
            $(".title").show();
        }
    }).change(); //trigger this on page load
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
    <div role="radiogroup">
        <input type="radio" name="gender" value="home" checked> Male
        <br>
        <input type="radio" name="gender" value="female"> Female
        <br>
        <input type="radio" name="gender" value="other"> Other
        <br />
        <br />
        <div class='title'>
            This is the title not on home value
        </div>
    </div>

    【讨论】:

      【解决方案3】:

      If Radio button checked show div 的副本

      这是为您的案例改编的代码:

      $('input[type="radio"]').click(function(){
          if($(this).attr("value")=="home"){
              $(".title").hide('slow');
          }
          else{
             $(".title").show('slow');
          }  
      });
      $('input[type="radio"]').trigger('click');
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <div role="radiogroup">
          <input type="radio" name="gender" value="home" checked> Male
          <br>
          <input type="radio" name="gender" value="female"> Female
          <br>
          <input type="radio" name="gender" value="other"> Other
          <br />
          <br />
          <div class='title'>
              This is the title not on home value
          </div>
      </div>

      【讨论】:

        【解决方案4】:

        试试这个。 首先在准备功能检查输入是否被检查。然后检查输入更改功能。

        $(document).ready(function() {
            var test = $("input[name='gender']");
            var home = $("input[value='home']")[0];
            var title = $(".title");
            if(home.checked) {
                title.hide();
            }
        
            test.on('change', function(e) {
                if(e.target.value === 'home') {
                title.hide();
              } else {
                title.show();
              }
            })
        });
        

        【讨论】:

          【解决方案5】:

          检查是否有任何输入值为checked

          // hide show title on load
          if ($("input[value='home']:checked").length) {  // can use :checked selector to see if input with value of home is checked (see link above)
            $(".title").hide();
          } else {
            $(".title").show();
          }
          
          $("input").on('change', function() {             // hide or show title on change of input (probably use a class here instead of a bare input selector)
            if (this.value === "home" && this.checked) {
              $(".title").hide();                          // hide title if value is home and checkbox is checked
            } else {
              $(".title").show();                          // otherwise show title
            }
          })
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <div role="radiogroup">
            <input type="radio" name="gender" value="home" checked> Male
            <br>
            <input type="radio" name="gender" value="female"> Female
            <br>
            <input type="radio" name="gender" value="other"> Other
            <br />
            <br />
            <div class='title'>
              This is the title not on home value
            </div>

          【讨论】:

          • 谢谢,成功了。
          猜你喜欢
          • 1970-01-01
          • 2011-10-21
          • 1970-01-01
          • 1970-01-01
          • 2016-03-10
          • 1970-01-01
          • 2018-10-30
          • 2013-07-22
          • 1970-01-01
          相关资源
          最近更新 更多