【问题标题】:Hide or show input field based on checkbox根据复选框隐藏或显示输入字段
【发布时间】:2015-09-17 04:44:57
【问题描述】:

我得到了这个脚本,它在小提琴上运行得非常好,但在我的网站上却不行。

http://jsfiddle.net/P2Ab2/30/

现场代码:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(".hiddenInput").hide();
$(".showHideCheck").on("change", function() {
    $this = $(this);
    $input = $this.parent().find(".hiddenInput");
    if($this.is(":checked")) {
        $input.slideDown();
    } else {
        $input.slideUp();
    }
});
</script>
</head>
<body>
<form>
    <div class="option">
        <input type="checkbox" name="chkBox1" id="chkBox1" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput"> Enter
        <input type="text" name="txtBox1"/></div>
    </div>
    <div class="option">
                <input type="checkbox" id="chkBox2" name="chkBox2" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput">Enter
        <input type="text" name="txtBox2"/></div>
    </div>
</form>
</body>
</html>

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

问题是您甚至在将元素加载到页面中之前就尝试使用它们。因此,有两种更简单的方法可以解决您的问题。

第一

您可以使用jQuery's document ready event:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() { // encapsulate your code into a function and pass it to jQuery
  $(".hiddenInput").hide();
  $(".showHideCheck").on("change", function() {
    $this = $(this);
    $input = $this.parent().find(".hiddenInput");
    if($this.is(":checked")) {
        $input.slideDown();
    } else {
        $input.slideUp();
    }
  });
});
</script>
</head>
<body>
<form>
    <div class="option">
        <input type="checkbox" name="chkBox1" id="chkBox1" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput"> Enter
        <input type="text" name="txtBox1"/></div>
    </div>
    <div class="option">
                <input type="checkbox" id="chkBox2" name="chkBox2" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput">Enter
        <input type="text" name="txtBox2"/></div>
    </div>
</form>
</body>
</html>

第二

您可以保持您的代码完整,但将脚本放在正文标记的末尾:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
<form>
    <div class="option">
        <input type="checkbox" name="chkBox1" id="chkBox1" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput"> Enter
        <input type="text" name="txtBox1"/></div>
    </div>
    <div class="option">
                <input type="checkbox" id="chkBox2" name="chkBox2" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput">Enter
        <input type="text" name="txtBox2"/></div>
    </div>
</form>
<script type="text/javascript">
$(".hiddenInput").hide();
$(".showHideCheck").on("change", function() {
    $this = $(this);
    $input = $this.parent().find(".hiddenInput");
    if($this.is(":checked")) {
        $input.slideDown();
    } else {
        $input.slideUp();
    }
});
</script>
</body>
</html>

【讨论】:

  • 谢谢。双向工作。
  • @MohitChandak 所以,请点击箭头为我的答案投票,然后在 V 中接受它:)
【解决方案2】:

将其包装在一个文档中。准备就绪:

$(document).ready(function(){

    $(".hiddenInput").hide();
        $(".showHideCheck").on("change", function() {
            $this = $(this);
            $input = $this.parent().find(".hiddenInput");
            if($this.is(":checked")) {
               $input.slideDown();
           } else {
              $input.slideUp();
           }
       });
 });

【讨论】:

    【解决方案3】:

    你正在执行

    $(".hiddenInput").hide();
    

    在标题上,在元素加载之前。尝试将脚本放在 body 元素之后:

    ...
        <div class="option">
                <input type="checkbox" id="chkBox2" name="chkBox2" class="showHideCheck" />Click me to show the text box
        <br/><div class="hiddenInput">Enter
        <input type="text" name="txtBox2"/></div>
    </div>
    <script type="text/javascript">
        $(".hiddenInput").hide();
        $(".showHideCheck").on("change", function() {
            $this = $(this);
            $input = $this.parent().find(".hiddenInput");
            if($this.is(":checked")) {
                $input.slideDown();
            } else {
                $input.slideUp();
            }
        });
    </script>
    

    或者设置一个函数在加载事件时执行:

    <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        function start() {
            $(".hiddenInput").hide();
            $(".showHideCheck").on("change", function() {
                $this = $(this);
                $input = $this.parent().find(".hiddenInput");
                if($this.is(":checked")) {
                    $input.slideDown();
                } else {
                    $input.slideUp();
                }
            });
        }
    </script>
    </head>
    <body onload="start()>
        ...
    </body>
    

    【讨论】:

    • 我该怎么做?加载事件时执行的函数?
    • 这是用纯javascript制作的。如果你想要一个 JQuery 解决方案,@Buzinas 的答案就是这样做
    【解决方案4】:

    如果您的网站恰好是 wordpress 网站,请查看 jQuery JavaScript not working on Wordpress, confused about no-conflict mode syntax。

    如果是wordpress,试试:

      jQuery(document).ready(function( $ ) {
    
       // $ Works! You can test it with next line if you like
       // console.log($);
       // put code here
    
       });
    

    如果你不使用 wordpress,试试这个:

    $( document ).ready(function() {
      // put code here
    });
    

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 2012-06-15
      • 2021-03-31
      • 2020-05-26
      • 2021-10-01
      • 2020-04-12
      • 1970-01-01
      • 2020-02-29
      相关资源
      最近更新 更多