简体   繁体   English

jQuery hide不隐藏vb.net/asp.net Webform中的任何内容

[英]jQuery hide doesn't hide any content in vb.net/asp.net webform

I am trying to hide my content by clicking the button. 我试图通过单击按钮隐藏我的内容。 But when the page is loading, content is hiding but after finishing page load content is showed. 但是,在页面加载时,内容是隐藏的,但是在完成页面加载后会显示内容。 This content doesn't hide. 此内容不会隐藏。 I am using Visual Studio 2017 community and asp.net/vb.net framework. 我正在使用Visual Studio 2017社区和asp.net/vb.net框架。 But the same JQuery code is working on my text editor and my browser. 但是,相同的JQuery代码也可以在我的文本编辑器和浏览器上运行。 Here is my sample code: 这是我的示例代码:

<p id="justp" class="testcls">Hi Hide me!</p>
    <asp:Button ID="testbtn" runat="server" Text="hide me" />
     <script>
        $(function () {
            $('#testbtn').click(function () {
                $('.testcls').hide();

            });
        });
    </script>

I am try to passing alert inside hide() and it is run successfully : 我试图在hide()内部传递警报,并且它成功运行:

$('.testcls').hide(alert("Hello"));

What is the problem? 问题是什么? How can I solve this? 我该如何解决?

In case your are just using this button to hide/show your content on the client side (no server side calculation or something like that) use a normal input / button tag: 如果您只是使用此按钮在客户端上隐藏/显示您的内容(不进行服务器端计算或类似的操作),请使用普通的input / button标签:

Replace this: <asp:Button ID="testbtn" runat="server" Text="hide me" /> 替换为: <asp:Button ID="testbtn" runat="server" Text="hide me" />

With this line: <button type="button" id="testbtn">Hide me!</button> 这行代码: <button type="button" id="testbtn">Hide me!</button>

As mentioned above this solution will only work if you don't need to submit any data. 如上所述,该解决方案仅在不需要提交任何数据时才有效。

What is the problem? 问题是什么?

The button submits your form after the click so the page will be refreshed and the view will return to the default status. 单击后,按钮将提交您的表单,因此页面将被刷新,视图将返回默认状态。

How can I solve this? 我该如何解决?

You've several ways to change this behavior : 您有几种更改此行为的方法:

  1. if you have got a control on the asp code you could remove the runat="server" to prevent the button from submitting : 如果您可以控制asp代码,则可以删除runat="server"以防止按钮提交:

     <asp:Button ID="testbtn" Text="hide me" /> 
  2. You could simply add the return false; 您可以简单地添加return false; statement at the end of the event. 事件结束时的声明。

  3. Another way is preventing the default behavior using preventDefault() like : 另一种方法是使用preventDefault()阻止默认行为,例如:

     $('#testbtn').click(function (e) { e.preventDefault(); $('.testcls').hide(); }); 
  4. The one that I recommend to you is using UseSubmitBehavior : 我建议您使用UseSubmitBehavior

     <asp:Button ID="testbtn" runat="server" UseSubmitBehavior="false" Text="hide me" /> 
  5. Last choice is using the plain HTML code like : 最后选择是使用纯HTML代码,例如:

     <button type="button" id="testbtn">Hide me!</button> 

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

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