简体   繁体   English

在Jquery中隐藏并显示div

[英]Hiding and showing div in Jquery

I want to Hide a div On a button click. 我要隐藏div在按钮上单击。 Here I am having 2 divs (Login and container). 在这里,我有2个div(登录和容器)。 When the page loads, container will be hidden. 页面加载时,容器将被隐藏。 And On click on a #submit button, I want to show the container div and hide the Login div. 然后单击#submit按钮,我要显示容器div并隐藏Login div。

The problem is Now login page is showing when the page loads, but After clicking the button I cannot make the div hide or show 问题是页面加载时现在显示登录页面,但是单击按钮后,我无法隐藏或显示div

Javascript: 使用Javascript:

<script>
    $('#submit').click(function() {
        console.log("Called2")

        $('#container').show();
        $('#login').hide();
    });
</script>

HTML: HTML:

<div id="login" >
    <div id="loginformcontainer">
        <form>
            <label> Username:
                <input type="text" name="uname">
            </label>
            <label> Password:
                <input type="password" name="pwd">
            </label>
            <div class="buttons1">
                <button type="button" id="submit" value="Login" class="btn">
                    Submit
                </button>
                <button type="reset"  id="reset" value="Reset"  class="btn">
                    Clear All
                </button>
            </div>
            <div>
            <div id="container" style="display: none;">
                <p> Index Page</p>
                    </div>

Write your code in document.ready function so it work, document.ready function编写您的代码,使其正常工作,

$(document).ready(function(){
    $('#submit').click(function() {
       console.log("Called2")
       $('#container').show();
       $('#login').hide();
    });
});

or, its shorthand, 或者,它的简写,

$(function(){
    $('#submit').click(function() {
       console.log("Called2")
       $('#container').show();
       $('#login').hide();
    });
});

for changing its visibility every time you can use toggle() function like, 每次您可以使用toggle() function来更改其可见性时,

$(function(){
    $('#submit').click(function() {
       console.log("Called2")
       $('#container, #login').toggle();
    });
});

See if this is the issue, you have not a closing </div> tag here: 看看是否是问题所在,这里没有</div>标记:

   </div>
   <div> //<----------------here you can see an opening tag instead
      <div id="container" style="display: none;">

put a closing </div> above. 在结束处</div>

and try putting your script in doc ready : 并尝试将脚本放入doc ready

 $(function(){
    $('#submit').click(function() {
      console.log("Called2")
      $('#container').show();
      $('#login').hide();
    });
 });

Look on the demo 演示

$('#container').hide();
$('#submit').click(function() {
    console.log("Called2")

    $('#container').show();
    $('#login').hide();
})

I believe that you not declare the <div id="container" ></div> 我相信您不会声明<div id="container" ></div>

Write your script in document.ready inside the <script> function so it work... <script>函数内部的document.ready编写脚本,这样可以正常工作...

Or you can't declare the library of Jquery. 否则您无法声明Jquery库。

<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.0.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

As it is a submit button handling the forms, submit event might be more useful. 由于它是处理表单的提交按钮,因此提交事件可能更有用。 In this way you can validate and prevent from submitting. 这样,您可以验证并阻止提交。

Please check the jQuery docs for submit . 请检查jQuery文档提交

Your html 您的html

 <div id="signIn" >Sign In</div>
<div id="login" >
<div id="loginformcontainer">

                <form>
                    <label> Username:
                        <input type="text" name="uname">
                    </label>
                    <label> Password:
                        <input type="password" name="pwd">
                    </label>
                    <div class="buttons1">
                        <button type="button" id="submit" value="Login" class="btn">
                            Submit
                        </button>
                        <button type="reset"  id="reset" value="Reset"  class="btn">
                            Clear All
                        </button>
      </div>
 <div>

Your Jquery 你的jQuery

 $('#login').hide();
    $('#signIn').click(function() {
        alert("try");
    });

    $("#submit").click(function() { 
         $('#login').fadeOut();
        //do your code
    });

Upon page loads, the login div will hide and if you click the sign in then login div will just fade in. If the user clicks the submit button, before you hide the login div, make sure you will validate the user action. 页面加载后,登录div会隐藏,如果您单击登录,登录div就会淡入。如果用户单击提交按钮,则在隐藏登录div之前,请确保将验证用户操作。 Refer to jquery documentation for further explanation. 请参阅jquery文档以获取更多说明。

不是提交按钮导致回发,因此您失去了由javascript设置的CSS样式。

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

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