asp.net jquery 按钮单击防止回发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14755731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:53:29  来源:igfitidea点击:

asp.net jquery button click prevent postback

jqueryasp.netpostback

提问by Arif Yilmaz

I would like to have asp:Button and when I click it, I dont want it to do postback but add a new div into . here is my code

我想要 asp:Button,当我单击它时,我不希望它进行回发,而是将一个新的 div 添加到 . 这是我的代码

    <script type="text/javascript">
        $(document).ready(function (e) {
            $('#<%=btn_comment_add.ClientID %>').click(function (e) {
                $('#comments').append('<div>asd</div>');
                return false;
            });
        });
    </script>

here is the asp.net button

这是asp.net按钮

  <asp:Button ID="btn_comment_add" runat="server" Text="G?nder" CssClass="theme04" UseSubmitBehavior="false" />

I can see that code adds the div but it postbacks so it gets deleted. I see the new div for a second only:)

我可以看到该代码添加了 div,但它回发,因此它被删除。我只看到了新的 div 一秒钟:)

please help me to fix it so it wont postback. Thank you very much people

请帮我修复它,这样它就不会回发了。非常感谢人们

回答by Ankit

instead of using ASP Buttonuse simple HTML button and assign jqueryfunction that you wish to run,

而不是ASP Button使用简单的 HTML 按钮并分配jquery您希望运行的功能,

<input type="button" name="btnname" onclick="MyJueryFunction()" />

and write you jquery as

并将你的 jquery 写为

function  MyJueryFunction()
{
  $('#comments').append('<div>asd</div>');

 }

回答by aquinas

If you don't want it to postback, then why do you have an asp.net button? In any case, you can do this:

如果您不希望它回发,那么为什么要使用 asp.net 按钮?在任何情况下,您都可以这样做:

  <asp:Button ID="btn_comment_add" runat="server" Text="G?nder" CssClass="theme04" UseSubmitBehavior="false" 
OnClientClick="return false;" />

回答by Tim B James

Get rid of the UseSubmitBehavior.

摆脱UseSubmitBehavior.

When you have this set to false, ASP.NET will add a client side onclickevent which will fire before/after your own javascript event, so no matter what you do within your click event to try and stop it from posting back, it will.

当您将此设置为 false 时,ASP.NET 将添加一个客户端onclick事件,该事件将在您自己的 javascript 事件之前/之后触发,因此无论您在单击事件中做什么以尝试阻止它回发,它都会。

View the source of the page, you will see something like;

查看页面的源代码,你会看到类似的东西;

javascript:__doPostBack(&#39;btnID&#39;,&#39;&#39;)

On the button

在按钮上

回答by shana

there are several ways you can achieve this. Following is another way

有几种方法可以实现这一点。以下是另一种方式

    $("#<%=ButtonClick.ClientID %>").bind('click', function (e) {
         e.preventDefault();
         //Do Your work 
    });

回答by Praneeth

IN THIS CASE ASP BUTTON HAVE TO CALL FUNCTION WITH SOME VALIDATION //ASP BUTTON

在这种情况下,ASP 按钮必须通过一些验证来调用函数 //ASP 按钮

asp:Button ID="btnSave" runat="server" Text="Save" class="btn btn-success" OnClientClick="return Valid()" OnClick="btnSave_Click"

asp:Button ID="btnSave" runat="server" Text="Save" class="btn btn-success" OnClientClick="return Valid()" OnClick="btnSave_Click"

function Valid() {

功能有效(){

        if ($("#ctl00_ContentPlaceHolder1_ddlLocation").val() == 0) {
            alert("Please select Location");
            $("#ctl00_ContentPlaceHolder1_ddlLocation").focus();
            return false;
        }

        if ($("#ctl00_ContentPlaceHolder1_txtArea").val().length == 0) {
            alert("Please Type Area");
            $("ctl00_ContentPlaceHolder1_txtArea").focus();
            return false;
        }
        return true;
    }