简体   繁体   English

按Div ID显示/隐藏

[英]Show/ Hide by Div Id

I have several fields in a form and if a user clicks on one field I want to display a table otherwise if they click another field the table should be hidden. 我在表单中有几个字段,如果用户单击一个字段,我想显示一个表,否则,如果用户单击另一个字段,则该表应该被隐藏。

I tried this javascript but when other fields are clicked it doesn't hide the table: 我试过这个javascript,但是当单击其他字段时,它不会隐藏表格:

<script>
function myFunction() {
    if (document.getElementById("userform")) {
        document.getElementById("userinfo").style.display="block";
    } else {
        document.getElementById("userinfo").style.display="none";
    }
}

</script>   

On each field I have included a onclick="myFunction()" within each input type. 在每个字段上,我在每种输入类型中都包含一个onclick="myFunction()" The table is set to style="display:none" 该表设置为style="display:none"

This is the HTML: 这是HTML:

    <form class="form-horizontal" role="form">
        <div class="form-group">
            <label for="hest" class="col-sm-1 control-label"><b>Pi</b></label>
            <div class="col-sm-10">
                <input type="tel" class="form-control" id="userform"
                    placeholder="Enter your unique Pi identity" onclick="myFunction()">
            </div>
        </div>
        <div class="form-group">
            <label for="inputPassword1" class="col-sm-1 control-label">
                 <b>Password</b>
            </label>
            <div class="col-sm-10">
                <input type="password" class="form-control" id="inputPassword1"
                    placeholder="Password" onclick="()">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-sample btn-lg">
                    <b>Submit</b>
                </button>
            </div>
        </div>
    </form>





    <div id="userinfo" class="collapse in" style="display:none">
        <table class="table table-bordered" id="phone-table">
            <tbody>
                <tr>
                    <td class="col-md-1"><button type="button"
                            class="btn btn-primary btn-xs1">
                            &nbsp;<br> <b>1</b>
                        </button>
                    </td>
                    <td class="col-md-1">
                        <button type="button"
                            class="btn btn-primary btn-xs1">
                            <b>ABC<br>2
                            </b>
                        </button>
                    </td>
                    <td class="col-md-1"><button type="button"
                            class="btn btn-primary btn-xs1">
                            <b>DEF<br>3
                            </b>
                        </button>
                    </td>
                </tr>
                <tr>
                    <td class="col-md-1"><button type="button"
                            class="btn btn-primary btn-xs1">
                            <b>GHI<br>4
                            </b>
                        </button></td>
                    <td class="col-md-1"><button type="button"
                            class="btn btn-primary btn-xs1">
                            <b>JKL<br>5
                            </b>
                        </button></td>
                    <td class="col-md-1"><button type="button"
                            class="btn btn-primary btn-xs1">
                            <b>MNO<br>6
                            </b>
                        </button></td>
                </tr>
                <tr>
                    <td class="col-md-1"><button type="button"
                            class="btn btn-primary btn-xs1">
                            <b>PQRS<br>7
                            </b>
                        </button></td>
                    <td class="col-md-1"><button type="button"
                            class="btn btn-primary btn-xs1">
                            <b>TUV<br>8
                            </b>
                        </button></td>
                    <td class="col-md-1"><button type="button"
                            class="btn btn-primary btn-xs1">
                            <b>WXYZ<br>9
                            </b>
                        </button></td>
                </tr>
                <tr>
                    <td class="col-md-1"><button type="button"
                            class="btn btn-primary btn-xs1">
                            <b><i class="fa fa-arrow-circle-left"></i><br>Del</b>
                        </button></td>
                    <td class="col-md-1"><button type="button"
                            class="btn btn-primary btn-xs1">
                            <b>&nbsp;<br>0
                            </b>
                        </button></td>
                    <td class="col-md-1"><button type="button"
                            class="btn btn-primary btn-xs1">
                            <b><i class="fa fa-eraser"></i><br>Clr</b>
                        </button></td>
                </tr>
            </tbody>
        </table>
    </div>

since Jquery was among tags, i throw my cents in jquery 由于Jquery属于标签,因此我将美分投入了jquery

.hide(); / .show(); / .show(); implicitly sets dislay to block or none in css. 在CSS中隐式将dislay设置为blocknone设置。

$(function(){
 $('#close').on('click',function(){
   $('#danceforme').hide();
 });
 $('#open').on('click',function(){
   $('#danceforme').show();
 });
});

html html

<button id="close">Stop Dancing</button>
<button id="open">Dance for me</button>

<div id="danceforme">
   I am dancing
</div>

demo: http://jsfiddle.net/doniyor/5FT9x/ 演示: http : //jsfiddle.net/doniyor/5FT9x/

You have tagged jQuery also so in that case it will be 您还标记了jQuery因此在这种情况下

$("#id").show();

$("#id").hide();

Doniyor's Answer is great for jQuery. Doniyor的答案非常适合jQuery。 I want to help explain how to make your existing code work, since that will help you understand why it wasn't working, instead of just handing you a solution. 我想帮助您解释如何使现有代码正常工作,因为这将帮助您了解为什么它不起作用,而不仅仅是提供解决方案。

Simple Solution 简单的解决方案

The problem with myFunction() is the if statement - myFunction()的问题是if语句-

if (document.getElementById("userform"))

That checks to see if an element with ID "userform" exists in the DOM. 这将检查DOM中是否存在ID为“ userform”的元素。 If it does, the statement returns true. 如果是这样,则该语句返回true。 Since there's an input with ID "userform" in the HTML you provided, this will always be true. 由于您提供的HTML中有一个ID为“ userform”的输入,因此这始终是正确的。

Instead, you want to check if the element that triggered your function had the ID "userform", 相反,您想检查触发函数的元素是否具有ID“ userform”,

so the relevant HTML would look like this: 因此相关的HTML如下所示:

<div class="form-group">
    <label for="hest" class="col-sm-1 control-label"><b>Pi</b></label>
        <div class="col-sm-10">
            <input type="tel" class="form-control" id="userform"
             placeholder="Enter your unique Pi identity"  onclick="myFunction(this)" />
        </div>
    </div>
    <div class="form-group">
        <label for="inputPassword1" class="col-sm-1 control-label">
             <b>Password</b>
        </label>
        <div class="col-sm-10">
            <input type="password" class="form-control" id="inputPassword1"
            placeholder="Password" onclick="myFunction(this)"/>
        </div>
    </div>

And myFunction() would look like this: myFunction()看起来像这样:

function myFunction(obj) {
    if (obj.id == "userform") {
        document.getElementById("userinfo").style.display="block";
    } else {
        document.getElementById("userinfo").style.display="none";
    }
}

jsFiddle jsFiddle

An explanation of registering onclick events . 有关注册onclick事件的说明 Because you're registering the onclick via html, and not in a script, the triggering element does not become the owner of the function, which is what this would refer to. 因为你注册通过HTML的onclick,而不是一个脚本,触发元件不会成为功能,这是什么的所有者this将引用。 So we have to pass the object via a parameter, and check that object's ID. 因此,我们必须通过参数传递对象,并检查该对象的ID。

Of course, this is all very roundabout, and since you're already using jQuery and bootstrap, you probably don't want to manually show and hide things. 当然,这都是非常round回的事情,并且由于您已经在使用jQuery和bootstrap,因此您可能不想手动显示和隐藏内容。

Bootstrap + jQuery fancy solution Bootstrap + jQuery花式解决方案

The div you're trying to show/hide is a collapse , which bootstrap has functionality for already. 您要显示/隐藏的div是一个lapsed ,该bootstrap已经具有功能。

Bootstrap solution jsFiddle 引导解决方案jsFiddle

Explanation: First, I removed the style="display:none" and class in on the collapse element. 说明:首先,我删除了style="display:none"in折叠元素上进行了类化。 Class in on a collapse tells bootstrap to default the element to showing, while display:none overrides bootstrap's styling. 收起时的class in告诉bootstrap默认将元素设置为show,而display:none覆盖bootstrap的样式。 It is enough to use class="collapse" , which defaults to collapsed, or hidden. 使用class="collapse" ,默认为折叠或隐藏。

Next, I removed the onclick assignments, since we'll let bootstrap handle that. 接下来,我删除了onclick分配,因为我们将让引导程序处理该分配。

Finally, I replaced the existing script behavior with a script telling bootstrap the behavior we want: 最后,我用告诉启动所需行为的脚本替换了现有的脚本行为:

$("#userform").focusin(function(){alert("in"); $("#userinfo").collapse("show");});
$("#inputPassword1").focusin(function(){alert("out"); $("#userinfo").collapse("hide");});

I left the alerts in for debugging, always helpful when you're not sure what's going on! 我将警报留在调试中,当您不确定发生了什么时总是有帮助的!

So what is this doing? 那这是做什么的? Following Bootstrap's example usage , we want the collapse to show when we click the "userform" element. 按照Bootstrap的示例用法 ,我们希望在单击“ userform”元素时显示折叠。

First, we tell jquery we want to do something when the element with id userform gets focus. 首先,我们告诉jquery当id为userform的元素获得焦点时我们想做些事情。 That's $("#userform").focusin() via jQuery API docs . 通过jQuery API docs就是$("#userform").focusin()

Then we tell jquery what we want to do when that happens - we could specify a named function, like myFunction() , but in this case, it's just one line, so we create an inline function. 然后,我们告诉jquery这种情况发生时我们想做什么-我们可以指定一个命名函数,例如myFunction() ,但是在这种情况下,它只是一行,所以我们创建了一个内联函数。 That's function(){} . 那是function(){}

Inside that function we specify the Bootstrap behavior we need. 在该函数内,我们指定所需的Bootstrap行为。 From the Bootstrap docs , we know we can use the function collapse("show") to manually expand the collapse element. Bootstrap文档中 ,我们知道我们可以使用功能collapse("show")来手动展开折叠元素。 To tell Bootstrap which one, we just give jQuery the element id! 为了告诉Bootstrap哪一个,我们只给jQuery元素id! Together, that's $("#userinfo").collapse("show"); 在一起,就是$("#userinfo").collapse("show");

Putting that inside the function that's triggered, which is specified in our focusin() function, we get the final result: `$("#userform").focusin(function(){ $("#userinfo").collapse("show");}); 将其放入在focusin()函数中指定的触发函数中,我们得到最终结果:`$(“#userform”)。focusin(function(){$(“#userinfo”)。collapse(“节目”);});

Next, we do the same for the elements we want to trigger hiding the collapse element. 接下来,对要触发隐藏隐藏元素的元素执行相同的操作。 So we just modify that line for #inputPassword1 , and tell it to hide the collapse using collapse("hide") instead of "show" . 因此,我们只需要为#inputPassword1修改该行,并告诉它使用#inputPassword1 collapse("hide")而不是"show"来隐藏折叠。 Logical, right? 符合逻辑吧?

I find it very helpful when I'm programming behaviors like this to consult the documentation. 当我对此类行为进行编程以查阅文档时,我发现它非常有用。 There are usually examples of exactly what I want to do, which only require minor tweaking to get working on my sites. 通常有一些我想做的事的例子,只需要稍作调整就可以在我的网站上工作。

jQuery's documentation is here. jQuery的文档在这里。

Bootstrap's documentation is here. Bootstrap的文档在这里。

And when asking for help on SO, it's great if you can create a jsFiddle to isolate the problem from the complexities of the website you're producing. 当寻求SO方面的帮助时,如果您可以创建一个jsFiddle来将问题与所生产网站的复杂性隔离开来,那就太好了。 Then other users who want to help can create modified copies of your code to show. 然后,其他需要帮助的用户可以创建代码的修改后的副本以显示。 It's super helpful! 超级有帮助!

Cheers. 干杯。

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

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