javascript 为什么任何 html 按钮会导致 aspx 页面中的回发?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17995007/
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-10-27 10:23:13  来源:igfitidea点击:

Why any html button cause postback in aspx page?

javascripthtmlasp.netsharepoint

提问by mimo

I have created .aspx page on my SharePoint site and inserted within the page HTML button.

我在我的 SharePoint 网站上创建了 .aspx 页面并插入到页面 HTML 按钮中。

Example

例子

    <PublishingWebControls:editmodepanel PageDisplayMode="Display" runat="server" SuppressTag="True">
...
<button>Click Me!</button>
...
</PublishingWebControls:editmodepanel>

Every time I hit 'Click Me!' the post back occurs. This is not my desired behavior, but I have found a way how to not cause post backs. I added javascript code to onclick property <button onclick='return false;'>Click Me!</button>

每次我点击“点击我!” 回发发生。这不是我想要的行为,但我找到了一种不引起回传的方法。我在 onclick 属性中添加了 javascript 代码<button onclick='return false;'>Click Me!</button>

My question is, why the post back occurs, even if the button does not contain type="submit"property?

我的问题是,即使按钮不包含type="submit"属性,为什么会发生回发?

I checked also master page, which contains <form runat="server">and wraps all the content and there is also no action="page.aspx"property.

我还检查了母版页,其中包含<form runat="server">并包装了所有内容,但也没有任何action="page.aspx"属性。

回答by Dmytro

Check this link, http://www.w3schools.com/tags/tag_button.aspThere is a note on page, which says, that different browsers can use different default type for button, if you don't specify it by yourself. Seems like your browser uses "submit".

检查这个链接,http://www.w3schools.com/tags/tag_button.asp页面上有一个注释,说不同的浏览器可以使用不同的默认按钮类型,如果你不自己指定。似乎您的浏览器使用“提交”。

回答by Aamir

I know this is an old post. @Dmytro 's post leads in the right direction but is not the exact answer. To prevent the postback you need to make use of the type attribute of the button tag. i.e.

我知道这是一个旧帖子。@Dmytro 的帖子指向正确的方向,但不是确切的答案。为了防止回发,您需要使用按钮标签的 type 属性。IE

<button type="button">Click Me!</button>

<button type="button">Click Me!</button>