简体   繁体   English

如何在使用jQuery单击按钮后显示元素?

[英]How to show an element after clicking a button using jQuery?

I when to show a panel when user clicks the button, By default I set panel visibility = false in properties, So when the user clicks the button then the button should hide and that panel should display. 我何时在用户单击按钮时显示面板,默认情况下我在属性中设置面板visibility = false ,因此当用户单击该按钮时,该按钮应该隐藏,并且该面板应该显示。 How can I achieve this using jQuery? 我怎样才能使用jQuery实现这一目标?

I tried the following: 我尝试了以下方法:

 $(document).ready(function () { //$('#PanelRegisterForm').hide(); $('#btnRegister').click(function () { $('#btnRegister').fadeOut("slow", function(){ //$('#PanelRegisterForm').attr("visibility","visible"); //$('#PanelRegisterForm').fadeIn("slow"); //$('#PanelRegisterForm').show(); $('#PanelRegisterForm').css("visibility", "visible"); }); }) }); 
 <div class="container container-max-width"> <div class="row"> <div class="panel "> <div class="" style="text-align:center;padding-top:50px;"> <button type="button" class="btn btn-info btn-md" id="btnRegister" >Register</button> </div> <asp:Panel ID="PanelRegisterForm" class="panel-body" runat="server" Visible="False"> <div class="form-group"> <h2 style="text-align: center;">Register</h2> </div> <div class="form-group"> <label class="control-label " for="txtCompanyName">Company Name *</label> <asp:TextBox ID="txtCompanyName" runat="server" class="form-control required-input"></asp:TextBox> </div> <div class="form-group"> <label class="control-label" for="txtCRno">CR Number *</label> <asp:TextBox ID="txtCRno" runat="server" class="form-control required-input"></asp:TextBox> </div> <div class="form-group"> <label class="control-label" for="txtContactPersonName">Contact Person Name</label> <asp:TextBox ID="txtContactPersonName" runat="server" class="form-control"></asp:TextBox> </div> <div class="form-group"> <label class="control-label" for="txtMobileNumber">Mobile Number *</label> <asp:TextBox ID="txtMobileNumber" runat="server" class="form-control "></asp:TextBox> </div> <div class="form-group"> <label class="control-label" for="txtOfficeTel1">Office Telephone No 1</label> <asp:TextBox ID="txtOfficeTel1" runat="server" class="form-control required-input"></asp:TextBox> </div> <div class="form-group"> <label class="control-label" for="txtOfficeTel2">Office Telephone No 2</label> <asp:TextBox ID="txtOfficeTel2" runat="server" class="form-control "></asp:TextBox> </div> <div class="form-group"> <label class="control-label" for="txtDomain">Domain</label> <asp:TextBox ID="txtDomain" runat="server" class="form-control"></asp:TextBox> </div> <div class="form-group"> <label class="control-label" for="txtLocation">Location</label> <asp:TextBox ID="txtLocation" runat="server" class="form-control" TextMode="MultiLine" Height="150px"></asp:TextBox> </div> <div class="form-group"> <label class="control-label" for="txtPostBox">Post Box</label> <asp:TextBox ID="txtPostBox" runat="server" class="form-control"></asp:TextBox> </div> <div class="form-group"> <label class="control-label" for="txtZipCode">Zip Code</label> <asp:TextBox ID="txtZipCode" runat="server" class="form-control"></asp:TextBox> </div> <div class="form-group"> <asp:Button ID="btnRegisterSubmit" runat="server" class="btn btn-info btn-block" Text="Register"/> </div> <p class="form-group">By creating an account, you agree to our <a class="link-hover" href="#">Terms of Use</a> and our <a class="link-hover" href="#">Privacy Policy</a>.</p> <hr /> <p> Already have an account? <a class="link-hover" href="http://sso.godaddy.com/?app=email&realm=pass">Sign in</a> </p> </asp:Panel> </div> </div> </div> 

At first I tried to hide a panel by default using jquery like $('#PanelRegisterForm').hide(); 起初我尝试使用jquery来隐藏面板,如$('#PanelRegisterForm').hide(); . But It's flickering when page is loaded. 但是当页面加载时它会闪烁。 So then I set visiblity is false. 所以我设置visiblity是假的。

When i click the button it fade out successfully, But Panel is not visible. 当我单击按钮时,它会成功淡出,但Panel不可见。

Thanks 谢谢

That should do it. 应该这样做。 Remember to add the "fade" class (from bootstrap) to the panel you want to show (or hide). 请记住将“淡入淡出”类(从引导程序)添加到要显示(或隐藏)的面板。 What this does is it's applying opacity: 0 so that your element is hidden, but keeps its dimensions. 它的作用是应用不透明度:0,以便隐藏元素,但保持其尺寸。 (if you want to not keep the dimensions, use the class "hidden" instead). (如果您不想保留尺寸,请使用“隐藏”类)。 Try to avoid using the fadeOut or fadeIn jQuery methods. 尽量避免使用fadeOut或fadeIn jQuery方法。 Use CSS classes instead, it's faster/cleaner. 使用CSS类,它更快/更清洁。

 $(document).ready(function() { $('#btnRegister').click(function() { $(this).fadeOut("slow"); $('#PanelRegisterForm').toggleClass("in"); }); }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="container"> <div id="PanelRegisterForm" class="fade"> <div class="form-group"> <input class="form-control" placeholder="name" /> </div> <div class="form-group"> <input class="form-control" placeholder="lastname" /> </div> </div> <button class="btn btn-primary" id="btnRegister">Button</button> </div> 

I am not sure if ASP.NET is like Prado (Prado is built basing on ASP.NET idea). 我不确定ASP.NET是否像Prado (Prado是基于ASP.NET的想法而构建的)。 If they are same, here is my suggestion/question: Can you check what happen to the Panel when you set Visible = False, is the HTML appear in the html source code of the page? 如果它们相同,这里是我的建议/问题:当你设置Visible = False时,你能检查Panel发生了什么,HTML是否出现在页面的html源代码中?

  1. If it's not appear/existed (just like Prado does) then your jQuery code is useless, you have to use CSS solution for it (see 2. below). 如果它没有出现/存在(就像Prado那样)那么你的jQuery代码是没用的,你必须使用CSS解决方案(参见下面的2.)。
  2. Another solution is use CSS to hide/show element instead: you can make a CSS class like hidden: 另一种解决方案是使用CSS来隐藏/显示元素:您可以创建一个类似隐藏的CSS类:

.hidden{ display:none; .hidden {display:none; } }

add add it to PanelRegisterForm initialy, then when you want that panel visible, simple use: 添加添加到PanelRegisterForm initialy,然后当你想要该面板可见时,简单使用:

$('#PanelRegisterForm').removeClass('hidden') $( '#PanelRegisterForm')。removeClass( '隐藏')

this will make no flickering because CSS is really fast. 这将不会闪烁,因为CSS非常快。

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

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